码迷,mamicode.com
首页 > 其他好文 > 详细

String.valueOf(null) 报空指针

时间:2016-08-05 19:44:26      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

String.valueOf 默认的方法

技术分享

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) 会调用更为具体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);

 

String.valueOf(null) 报空指针

标签:

原文地址:http://www.cnblogs.com/zno2/p/4545835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!