想要知道new Long(String s)和Long.valueOf(String s)的区别,还是得从源码直接看比较直观
new Long(String s)
Long.valueOf(String s)
从源码可以看出,使用new Long(s)和Long.valueOf(s)都会去调用parseLong(s, 10)方法,
parseLong(String s, int radix)
/**
* Parses the string argument as a signed {@code long} in the
* radix specified by the second argument. The characters in the
* string must all be digits of the specified radix (as determined
* by whether {@link java.lang.Character#digit(char, int)} returns
* a nonnegative value), except that the first character may be an
* ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to
* indicate a negative value or an ASCII plus sign {@code '+'}
* ({@code '\u005Cu002B'}) to indicate a positive value. The
* resulting {@code long} value is returned.
*
* <p>Note that neither the character {@code L}
* ({@code '\u005Cu004C'}) nor {@code l}
* ({@code '\u005Cu006C'}) is permitted to appear at the end
* of the string as a type indicator, as would be permitted in
* Java programming language source code - except that either
* {@code L} or {@code l} may appear as a digit for a
* radix greater than or equal to 22.
*
* <p>An exception of type {@code NumberFormatException} is
* thrown if any of the following situations occurs:
* <ul>
*
* <li>The first argument is {@code null} or is a string of
* length zero.
*
* <li>The {@code radix} is either smaller than {@link
* java.lang.Character#MIN_RADIX} or larger than {@link
* java.lang.Character#MAX_RADIX}.
*
* <li>Any character of the string is not a digit of the specified
* radix, except that the first character may be a minus sign
* {@code '-'} ({@code '\u005Cu002d'}) or plus sign {@code
* '+'} ({@code '\u005Cu002B'}) provided that the string is
* longer than length 1.
*
* <li>The value represented by the string is not a value of type
* {@code long}.
* </ul>
*
* <p>Examples:
* <blockquote><pre>
* parseLong("0", 10) returns 0L
* parseLong("473", 10) returns 473L
* parseLong("+42", 10) returns 42L
* parseLong("-0", 10) returns 0L
* parseLong("-FF", 16) returns -255L
* parseLong("1100110", 2) returns 102L
* parseLong("99", 8) throws a NumberFormatException
* parseLong("Hazelnut", 10) throws a NumberFormatException
* parseLong("Hazelnut", 36) returns 1356099454469L
* </pre></blockquote>
*
* @param s the {@code String} containing the
* {@code long} representation to be parsed.
* @param radix the radix to be used while parsing {@code s}.
* @return the {@code long} represented by the string argument in
* the specified radix.
* @throws NumberFormatException if the string does not contain a
* parsable {@code long}.
*/
public static long parseLong(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
long result = 0;
boolean negative = false;
int i = 0, len = s.length();
long limit = -Long.MAX_VALUE;
long multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Long.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
parseLong(String s, int radix)返回long值,这事区别就出来了,对于new Long(String s)是直接将该long值返回,而Long.valueOf(String s)会将返回的long值继续处理Long.valueOf(long l)
那么从valueOf(long l)方法可以看到再返回结果之前会进行一个判断,判断当值在[-128,127]之间返回的是缓存的值,不在这个值区间了才会通过构造函数返回,从这一点上看会比new Long(String s)更节省空间,提高性能。