String常量池使用**.equals()**中的空指针异常
当我们执行如下
package com.hanxiao;
public class StringUse {
public static void main(String[] args) {
String str1 = "abc";
System.out.println(str1.equals("abc"));//true 推荐使用
System.out.println("abc".equals(str1));//true 不推荐使用
}
}
至于第二种方式为何不推荐,请看下面代码:
package com.hanxiao;
public class StringUse {
public static void main(String[] args) {
String str1 = null;
System.out.println(str1.equals("abc")); //true 推荐使用
System.out.println("abc".equals(str1)); //空指针
}
}
大家请看