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.
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.999999999999999E22IdentityHashMap remove(key, value) and replace(key, value, newValue) error handling
IdentityHashMapuses 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;Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
