Bhavesh
Bhavesh

Reputation: 4677

What is the best approach to parse a String type to other numeric types available in Java?

Let's consider the following code snippet in Java. There are some of possible approaches (that I know) to parse a String value to other numeric types (Let's say for the sake of simplicity, it is an Integer, a wrapper type in Java).

package parsing;

final public class Parsing
{
    public static void main(String[] args)
    {
        String s="100";

        Integer temp=new Integer(s);
        System.out.print("\ntemp = "+temp);

        temp=Integer.parseInt(s);
        System.out.print("\ntemp = "+temp);        

        temp=Integer.valueOf(s).intValue();
        System.out.print("\ntemp = "+temp);

        temp=Integer.getInteger(s);
        System.out.print("\ntemp = "+temp);
    }    
}

In all the cases except the last one, returns the value 100 after converting it into an Integer. Which one is the best approach to parse a String value to other numeric types available in Java? The last case returns NULL even if the String object s already contains a parsable value. Why?

Upvotes: 2

Views: 175

Answers (8)

shift66
shift66

Reputation: 11958

See getInteger(), which "determines the integer value of the system property with the specified name."

Upvotes: 0

COD3BOY
COD3BOY

Reputation: 12112

getInteger(String s) returns the integer value of the system property with the specified name.If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned. Note that this is equivalent to getInteger(s, null) evidently its not doing what you expect it to do!


Now analyzing the remaining approaches, valueOf() result is an Integer object that represents the integer value specified by the string,an Integer object equal to the value of: new Integer(Integer.parseInt(s)), so lets rule out this first.

Now the new Integer(s) approach, now if you want to work with the int value, you again need to get the intValue(), So finally it turns out that Integer.parseInt(s) approach is better (depends).

Javadoc can provide you with more details in case you need.

Upvotes: 1

Pankaj Kumar
Pankaj Kumar

Reputation: 83018

Integer.getInteger(String) says that

Determines the integer value of the system property with the specified name.

If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned. In other words, this method returns an Integer object equal to the value of:

getInteger(nm, null)

And here is best explanation of your confusion.

Edited :

AFAIK:

  • Integer.valueOf(String) converts a String to a number by assuming the String is a numeric representation. In other words. Integer.valueOf("100") yields the number 100.
  • Integer.getInteger(String) converts a String to a number by assuming the String is the name of a system property numeric representation. In other words. Integer.getInteger("100") is likely to yield null.

Upvotes: 1

Stephen C
Stephen C

Reputation: 719259

Calling Integer.getInteger("42") attempts to fetch the value of the system property whose name is "42". Unless you really do have a system property with that name, it will return null.

Here is the Java 7 javadoc if you want more details.

Yes, the name of the method is misleading, and its utility is questionable.


FWIW, I'd use Integer.parseInt(String) if I required an int result and Integer.valueOf(String) if I required an Integer. I'd only use new Integer(String) if I required the object to be a new Integer and not one that might be shared.

(The reasoning is the same as for new Integer(int) versus Integer.valueOf(int). Again, read the javadocs for a more complete explanation.)

Upvotes: 4

Prince John Wesley
Prince John Wesley

Reputation: 63698

Integer.parseInt(s) is the best approach.

Interger#valueOf() method will return an Integer object equal to the value of new Integer(Integer.parseInt(s)).

Integer.getInteger() is not even a candidate.

Upvotes: 1

wannik
wannik

Reputation: 12726

I prefer parseInt(...).

The reason that your last output is null is because getInteger(...) get the integer value from a property. To set the value of a property, use an option -D. Therefore, if you use the command line

java -D100=5 Test

You will not get the null but you'll get 5. Here, the option -D sets the property named "100" to value "5".

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

The last case returns NULL even if the String object s already contains a parsable value. Why?

It's not doing what you think it's doing:

public static Integer getInteger(String nm)

Determines the integer value of the system property with the specified name.

See API Doc.

Also see the doc for other related methods, they do different things.

 new Integer(s); //you are creating new object
 parseInt(s); // parses and returns int (primitive)
 valueOf(s); //returns cached Integer object with the int value in s

So, it depends: if you need int then parseInt(s) and if you need Integer then valueOf(s).

Upvotes: 1

James.Xu
James.Xu

Reputation: 8295

temp=Integer.parseInt(s);

unlike the other it just calculate a int, not a Integer. (this is not a big advantage though)

Upvotes: 1

Related Questions