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:


and the docs for valueOf() here:


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? Thanks!

Difference between = and new

What is the difference between this: Object one = new Object(); Object two = one; and this: Object one = new Object(); Object two = new Object(); two = one; What would be the effect on object one wh

Difference between ‘new operator’ and ‘operator new’?

What is difference between new operator and operator new? Thanks.

Difference between new and initialize in Smalltalk?

A newbie question, what is the difference between new and initialize? Oscar

Difference between operator new() and operator new[]()?

Is there any difference between fncs: operator new and operator new[] (NOT new and new[] operators)? Except of course call syntax? I’m asking because I can allocate X number of bytes for my objs with

Difference between new xxxBorder() and Borderfactory method?

What is the difference between setBorder(new TitledBorder()); and setBorder(BorderFactory.createTitledBorder()); and which advantages/disadvantages do they have?

Difference between “new” operator and “new” function [duplicate]

This question already has an answer here: Difference between ‘new operator’ and ‘operator new’? 9 answers Interview question: what’s the difference between new operator and new function? I

Difference between new Test() and new Test() { }

What is the difference between these two ways of instantiating new objects of a class as follows: Test t1=new Test(); Test t2=new Test(){ }; When I tried the following code, I could see that both obj

Difference between “{}” and “new Object()” [duplicate]

Possible Duplicate: creating objects – new object or object literal notation? What exactly is the difference between the following: var myData = new Object(); myData[name] = ATOzTOA; myData[sit

Difference between “New Folder”, “New Source Folder” and “New Package”?

When you right-click on a project in eclipse, what is the difference between New Folder, New Source Folder and New Package? They all seem to do the same thing and the reference does not say much

difference between Function and new Function

I sometimes see people doing this Function(‘alert(hi)’) and sometimes they do new Function(‘alert(hi)’) Is there a difference between the two?


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

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.

Both do parseLong(String, int) internally (int being radix with value as 10), but valueOf has advantage as documented below:

If a new Long instance is not required, this method should generally be used in preference to the constructor Long(long), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

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);

This is the PMD plugin out put which is run on eclipse

Code I checked is

Long l = new Long("123456");

In JDK 1.5, calling new Long() causes memory allocation. Long.valueOf() is more memory friendly.

i am thinking how to change range and size of the cache for our application, overloaded with Longs;

such change is not supported by j2se api One way is to change loaded java byte code with ClassLoader or even with JVMTI (it allows to keep such trick out of the project, like external tuning)

or, may be, to create external cache and own static cachedValueOf() which is straight forward, but the code depending on some not applicational needs is not nice