博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String.valueOf(null) 报空指针
阅读量:4965 次
发布时间:2019-06-12

本文共 1347 字,大约阅读时间需要 4 分钟。

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 char 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 char 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);

 

转载于:https://www.cnblogs.com/zno2/p/4545835.html

你可能感兴趣的文章
[Compose] 10. Capture Side Effects in a Task
查看>>
[Javascript AST] 0. Introduction: Write a simple BabelJS plugin
查看>>
[Core Javascirpt] Basic Metaprogramming: Dynamic Method
查看>>
[Angular2 Router] Use Params from Angular 2 Routes Inside of Components
查看>>
makefile
查看>>
Spring 构造注入和Set注入复习
查看>>
<mvc:annotation-driven/>的作用
查看>>
服务器一:分布式服务器结构
查看>>
迭代dict的value
查看>>
eclipse package,source folder,folder区别及相互转换
查看>>
Py 可能是最全面的 python 字符串拼接总结(带注释版)
查看>>
如何从亿量级中判断一个数是否存在?
查看>>
客户数据(类的调用)
查看>>
cookie session 和登录验证
查看>>
为mysql数据库建立索引
查看>>
语言-汉语-官话-中原官话-兖菏片:兖菏片
查看>>
HTML-参考手册: 画布
查看>>
杂项:MIS
查看>>
Node.js:全局对象
查看>>
6、python中的字符串
查看>>