Fundamentals 3 min read

What Changed in Java 21? Double/Float toString Precision and IdentityHashMap Bug Fixes

Java 21 introduces critical bug fixes, correcting the precision of Double.toString() and Float.toString() outputs and fixing the erroneous value comparison in IdentityHashMap’s remove(key, value) and replace(key, value, newValue) methods, ensuring expected behavior across versions.

Programmer DD
Programmer DD
Programmer DD
What Changed in Java 21? Double/Float toString Precision and IdentityHashMap Bug Fixes

Double.toString() and Float.toString() precision fix

In Java 21, the precision issue of Double.toString() and Float.toString() has been fixed. For example, Double.toString(1e23) now produces the expected output.

In Java 19 and later, the output is: 1.0E23 In Java 18, the output was:

9.999999999999999E22

IdentityHashMap remove(key, value) and replace(key, value, newValue) error handling

IdentityHashMap

uses identity comparison (==) instead of equals() to determine key equality. Before Java 20, the remove(key, value) and replace(key, value, newValue) methods incorrectly compared the supplied value with the map’s stored value using equals(), leading to unexpected results.

Starting with Java 20, these methods correctly use identity comparison, so the operations behave as documented.

Example:

var users = new IdentityHashMap<String, User>();
String key = "abc";
users.put(key, new User("Jane Doe"));
var removed = users.remove(key, new User("Jane Doe"));
assert !removed;
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBugFixJava21DoubleToStringIdentityHashMap
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.