I would like to suggest using Long.parseLong
over every other option because :
- Every other function i.e
Long.valueOf
, Long(string)
relies on Long.parseLong
method which works as a core method for every String
to Long
conversion.
- As about caching, this will work when your input is only between -128 to 127 (see the Example below) so it's up to the coder that you want to call
Long.parseLong
directly or through Long.valueOf
when your object is of String
type.(obviously, use direct call).official docs and the source code link (why don't use Byte instead of Long then because cache won't save every long value even this range apply for Integers too)
From official docs
public static Long valueOf(String s)
throws NumberFormatException
Returns a Long object holding the value of the specified String. The argument is interpreted as representing a signed decimal long, exactly as if the argument were given to the parseLong(java.lang.String) method. The result is a Long object that represents the integer value specified by the string.
**In other words, this method returns a Long object equal to the value of:**
new Long(Long.parseLong(s))
// internally calls to Long.valueOf when you pass string
public static Long valueOf(String s) throws NumberFormatException
{
return Long.valueOf(parseLong(s, 10));
}
- About
Long.valueOf
returning a direct Wrapper object without creating new Long
object is a false statement , as according to internally use of Long.parseLong
(which returns a primitive long) , the primitive output of Long.parseLong
will be converted to Wrapper
object using by creating new object of Long
class hence you wanna use direct Boxing
or can call Long.valueOf=>Long.parseLong=>new Long
A little more about caching(if pass value is long) :
Cache is little helpful when you want to use == for equality check (like intern strings) with Object type. Long cache will only keep a static array of objects who's value is between -128 to 127
range inclusive so if your number is outside this range then you won't be able to use == operator for equality check (you don't believe, try following example)
Example:
Long b2=128L;
Long b3=128L;
Long aa=Long.valueOf("134");
Long ab=Long.valueOf("134");
System.out.println(b2==b3); // no cache for out of range values
System.out.println(aa==ab); // no cache for out of range values
System.out.println(aa.equals(ab)); // must use equals for equality check
System.out.println(b2.equals(b3));
b2=44; // between -128 to 127 it will work
b3=44;
System.out.println(b2==b3);
Output:
false
false
true
true
true
So try to use equals
for equality check.
Why cache required: because number between -128 to 127 inclusive need to be given identity for performance reasons of JLS (5.1.7) so cache is not for time/space efficiency in this case.
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache , range is clearly seen
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
Conclusion:
- Use
Long.parseLong
.
- Must use equals while working with Wrapper classes.
- Cache for java mechanism works only if you want to use numbers between -128 to 127 with
Wrapper
classes.