Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in behaviour between Double.parseDouble and Integer.parseInt [duplicate]

Tags:

java

It seems parseDouble can accept strings with trailing spaces, but parseInt and parseLong throw an Exception.

e.g. For this test case

@Test
public void testNumberParsing() {
    try {
        Double.parseDouble("123.0 ");
        System.out.println("works");
    }catch (NumberFormatException e) {
        System.out.println("does not work");
    }
    try {
        Integer.parseInt("123 ");
        System.out.println("works");
    }catch (NumberFormatException e) {
        System.out.println("does not work");
    }
    try {
        Long.parseLong("123 ");
        System.out.println("works");
    }catch (NumberFormatException e) {
        System.out.println("does not work");
    }
}

The results are

works
does not work
does not work

Why the difference in behaviour? Is that intentional?

like image 650
justAnotherGuy Avatar asked Feb 07 '18 17:02

justAnotherGuy


People also ask

What is the difference between double valueOf and double parseDouble?

valueOf() creates a Double object which is often not needed. parseDouble() does not. With autoboxing it's valueOf(String) which is no longer needed, but is therefore backward compatibility.

What does double parseDouble () do?

parseDouble. Returns a new double initialized to the value represented by the specified String , as performed by the valueOf method of class Double .

What is double parseDouble in Java?

Double parseDouble() method in Java with examples The parseDouble() method of Java Double class is a built in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double. Syntax: public static double parseDouble(String s)

Can you parseInt null?

If parseInt() is given an empty string or a null value it will also return NaN, rather than converting them to 0. This gives us a mechanism by which we can test values using a combination of parseInt() and isNaN().


2 Answers

This behaviour is actually documented (though this is quite a bad design...)!

Double.parseDouble:

Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

Double.valueOf:

Leading and trailing whitespace characters in s are ignored. Whitespace is removed as if by the String.trim() method; that is, both ASCII space and control characters are removed.

Integer.parseInt:

The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.

like image 111
Sweeper Avatar answered Nov 14 '22 22:11

Sweeper


From the source code of parseDouble

  in = in.trim(); // don't fool around with white space.

However this is not happening in case of parseInt. They are simply checking for null and proceeding further.

Agree with you. Authors should have done the same for Integer as well.

like image 36
Suresh Atta Avatar answered Nov 14 '22 22:11

Suresh Atta