JDK-8039168 : One implementation problem of Integer.parseInt(String str,int radix)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2_10,7u17
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2014-03-30
  • Updated: 2014-04-03
  • Resolved: 2014-04-03
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) Client VM (build 23.7-b01, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
In computer, negative integer use complement code to store. So Integer.MIN_VALUE= -2^31, Ineger.MAX_VALUE=2^31-1 and abs(Integer.MIN_VALUE) = abs(Integer.MAX_VALUE)+1.

If I use: Integer.toBinaryString(Integer.MIN_VALUE), then output 10000000000000000000000000000000 , it use complementation to represent.

The problem is as follows:
   If I use Integer.parseInt("10000000000000000000000000000000",2), it throws an exception, but ideally it should output -2147483648, that is Integer.MIN_VALUE.

I think Y=Integer.parseInt(X,2) and X=Integer.toBinaryString(Y) should be always true.

I think jdk may not implement this special condition.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
System.out.println(Integer.parseInt(Integer.toBinaryString(Integer.MIN_VALUE),2));

will show this bug.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
-2147483648
ACTUAL -
Exception in thread "main" java.lang.NumberFormatException: For input string: "10000000000000000000000000000000"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at Test.main(Test.java:3)


ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NumberFormatException: For input string: "10000000000000000000000000000000"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at Test.main(Test.java:3)


REPRODUCIBILITY :
This bug can be reproduced always.


Comments
The toBinaryString and related methods produce unsigned output, however, parseInt takes *signed* input. Therefore, it is not always possible to round-trip such values. Use the parseUnsignedInt method in Java SE 8 and later to avoid this problem (JDK-4215269).
03-04-2014

Seems like a corner case is being mishandled. But is a long standing issue. Checked with 1.4.2
03-04-2014

Looks like a valid issue - will let Dev determine.
03-04-2014