String.valueOf 默认的方法
argument 可以为null 的
- boolean b = null;
- char c = null;
- char[] data = null;
- double d = null;
- float f = null;
- int i = null;
- long l = null;
- Object obj = null;
String.valueOf(null) 会调用更为具体valueOf(char[] data)
/**
* Returns the string representation of the <code>char</code> array
* argument. The contents of the character array are copied; subsequent
* modification of the character array does not affect the newly
* created string.
*
* @param data a <code>char</code> array.
* @return a newly allocated string representing the same sequence of
* characters contained in the character array argument.
*/
public static String valueOf(char data[]) {
return new String(data);
} /**
* Allocates a new {@code String} so that it represents the sequence of
* characters currently contained in the character array argument. The
* contents of the character array are copied; subsequent modification of
* the character array does not affect the newly created string.
*
* @param value
* The initial value of the string
*/
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
会在value.length 处抛空指针异常!
case1:
String.valueOf(null);
case2:
char[] data;
String.valueOf(data);