Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Long.valueOf(java.lang.String) and new Long(java.lang.String)?

I'm consolidating code written by two different people and notice that casting a String value into a Long has been done in two different ways.

Coder #1 has done this:

String strId = "12345678";
...
Long lId = new Long(strId);

While coder #2 has done this:

String strId = "12345678";
...
Long lId = Long.valueOf(strId);

Functionally, the code operates exactly the same. There's a try/catch block around each bit to handle any NumberFormatException that is thrown. The incoming string value is an 8 digit string that represents a decimal: "12345678" and in both cases it is correctly converted into Long.

Is there any functional difference between passing the string in the constructor and using Long.valueOf()? I've checked the constructor doc here:

Long(java.lang.String)

and the docs for valueOf() here:

Long.valueOf(java.lang.String)

As far as I can tell, they both call parseLong() so it doesn't matter which is used. I just want to make sure I'm not setting myself up for some strange behavior further down the road. Also, is either style more "correct" (haha) than the other?

like image 874
AWT Avatar asked Oct 30 '12 18:10

AWT


People also ask

What is long valueOf in Java?

valueOf() is a built-in method in Java of lang class that returns a Long object holding the value extracted from a specified String S when parsed with the radix that is given in the second argument.

What is Java Lang Long?

The java. lang. Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.

How do you return a long value in Java?

Long. longValue() is an inbuilt method of the Long class in Java which returns the value of this Long object as a long after the conversion. Parameters: This method do not take any parameters. Return Value: This method will return the numeric value represented by this object after conversion to long type.


3 Answers

The difference is that using new Long() you will always create a new object, while using Long.valueOf(), may return you the cached value of long if the value is between [-128 to 127].

So, you should prefer Long.valueOf method, because it may save you some memory.

If you see the source code for Long.valueOf(String), it internally invokes Long.valueOf(long), whose source code I have posted below: -

public static Long valueOf(String s) throws NumberFormatException
{
    return Long.valueOf(parseLong(s, 10));
}

public static Long valueOf(long l) {
    final int offset = 128;
    if (l >= -128 && l <= 127) { // will cache 
        return LongCache.cache[(int)l + offset];
    }
    return new Long(l);
}
like image 153
Rohit Jain Avatar answered Oct 01 '22 23:10

Rohit Jain


Long.valueOf() should be preferred: it returns cached values of Long for some often-used values instead of constructing a new instance as the constructor does.

Even if some Java versions don't use a cache, using valueOf() makes it possible in future versions, whereas the constructor will always create a new instance.

like image 8
JB Nizet Avatar answered Oct 02 '22 01:10

JB Nizet


They mean the same

public static Long valueOf(String s) throws NumberFormatException{
        return new Long(parseLong(s, 10));
}

public Long(String s) throws NumberFormatException {
    this.value = parseLong(s, 10);
}

Source JDK 6.0

like image 3
Arun Manivannan Avatar answered Oct 02 '22 00:10

Arun Manivannan