Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8039168

One implementation problem of Integer.parseInt(String str,int radix)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2_10, 7u17
    • core-libs
    • x86
    • windows_7

      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.

            darcy Joe Darcy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: