The difference between Java empty string and null

String str = null; System. out. println(str.length());

if (str.length() == 0)

if (str.equals(""))

if (str == null)

if (str != null && str.length() != 0)

public static void main(String[] args) {
    String str1 = new String();
    String str2 = null;
    String str3 = "";

    System.out.println(str3.length()); // 空字符串""的长度为0
    System.out.println(str2.length()); // 抛出空指针异常

    System.out.println(str1); // 输出""
    System.out.println(str1 == str2); // 内存地址的比较,返回false
    System.out.println(str1.equals(str2)); // 值的比较,返回false
    System.out.println(str2 == str3); // 内存地址的比较,返回false
    System.out.println(str3.equals(str2)); // 值的比较,返回false
    System.out.println(str1 == str3); // 内存地址的比较,返回false
    System.out.println(str1.equals(str3)); // 值的比较,返回true
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>