RFR: 8370013: Refactor Double.toHexString to eliminate regex and StringBuilder

Joe Darcy darcy at openjdk.org
Mon Oct 20 04:37:02 UTC 2025


On Tue, 14 Oct 2025 23:26:00 GMT, Shaojin Wen <swen at openjdk.org> wrote:

> This PR refactors the Double.toHexString method to improve its performance by eliminating the use of StringBuilder and regex operations. The new implementation uses a direct byte array approach to construct the hex string representation, which avoids the overhead of StringBuilder operations and regex pattern matching.
> 
> Existing tests in `java/lang/Double/ToHexString.java`.

src/java.base/share/classes/java/lang/Double.java line 726:

> 724:                 long signifBits = (Double.doubleToLongBits(d)
> 725:                         & DoubleConsts.SIGNIF_BIT_MASK) |
> 726:                         0x1000000000000000L;

As a small syntax improvement, I think using underscores between groups of four digits would help how the count is correct (that syntax was legal when this code was written. Something like

 0x1000_0000_0000_0000L

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/27811#discussion_r2443794548


More information about the core-libs-dev mailing list