Difference between null and empty (“”) Java String

前端 未结 22 1694
再見小時候
再見小時候 2020-11-22 17:10

What is the difference between null and the \"\" (empty string)?

I have written some simple code:

String a = \"\";
String b         


        
22条回答
  •  萌比男神i
    2020-11-22 17:47

    Amazing answers, but I'd like to give from a different perspective.

    String a = "StackOverflow";
    String a1 = "StackOverflow" + "";
    String a2 = "StackOverflow" + null;
    
    System.out.println(a == a1); // true
    System.out.println(a == a2); // false
    
    

    So this can tell us "" and null point to the different object references.

提交回复
热议问题