JDK-4994234 : Inconsistencies in parseXXX() Parameter Handling
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-02-13
  • Updated: 2004-02-13
  • Resolved: 2004-02-13
Related Reports
Relates :  
Description

Name: rmT116609			Date: 02/12/2004


FULL PRODUCT VERSION :
java version "1.4.2_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)

java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
Each numeric wrapper class has a parseXXX() method. The parseFloat() and parseDouble() methods automatically trim their string parameters before attempting to convert them whereas the methods, parseByte(), parseShort(), parseInt() and parseLong() do not. Instead of trimming their string parameters, these methods throw a NumberFormatException.

Also, please note that the Boolean.valueOf() method does not trim its string paramter either. When you pass the string, "true\n", to this method it returns false.

I think there needs to be consistency. Either all parseXXX() methods should trim their string parameters before the attempted conversion or they should all throw NFE when presented with leading/trailing whitespace in their parameters.

I have a test case below to illustrate the problem.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
see the test case.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class Tester {
  public static void main(String[] args) throws Exception {
    String boolString = "true\n";
    String testString = " 4\n";

    // First we try boolean
    Boolean bool = Boolean.valueOf(boolString);
    System.out.println(bool);
    
    // Now we try byte
    try {
      byte b = Byte.parseByte(testString);
      System.out.println("byte is " + b);
    } catch(NumberFormatException e) {
      System.err.println("Got a NFE in Byte.parseByte()");
    }

    // Now we try short
    try {
      short s = Short.parseShort(testString);
      System.out.println("short is " + s);
    } catch(NumberFormatException e) {
      System.err.println("Got a NFE in Short.parseShort()");
    }
  
    // Now we try int
    try {
      int i = Integer.parseInt(testString);
      System.out.println("int is " + i);
    } catch(NumberFormatException e) {
      System.err.println("Got a NFE in Integer.parseInt()");
    }
    
    // Now we try long
    try {
      long l = Long.parseLong(testString);
      System.out.println("long is " + l);
    } catch(NumberFormatException e) {
      System.err.println("Got a NFE in Long.parseLong()");
    }
        
    // Now we try float
    try {
      float f = Float.parseFloat(testString);
      System.out.println("float is " + f);
    } catch(NumberFormatException e) {
      System.err.println("Got a NFE in Float.parseFloat()");
    }

    // Now we try double
    try {
      double d = Double.parseDouble(testString);
      System.out.println("double is " + d);
    } catch(NumberFormatException e) {
      System.err.println("Got a NFE in Double.parseDouble()");
    }
  } // main()
} // Tester
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Always trimg the string parameters.
(Incident Review ID: 234786) 
======================================================================

Comments
EVALUATION Yes, the behavior is gratuitiously inconsistent; however, the behavior is so long standing that is not practical to change it now. ###@###.### 2004-02-12
12-02-2004