String.valueOf(null) 报空指针

String.valueOf 默认的方法

String.valueOf(null) 报空指针

argument 可以为null 的

  1. boolean b = null;
  2. char c = null;
  3. char[] data = null;
  4. double d = null;
  5. float f = null;
  6. int i = null;
  7. long l = null;
  8. Object obj = null;

String.valueOf(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);

上一篇:更改Windows可执行文件的图标


下一篇:java Byte.toString 方法与String.ValueOf(Byte)效率比较