Difference between null and empty (“”) Java String

前端 未结 22 1700
再見小時候
再見小時候 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条回答
  •  情话喂你
    2020-11-22 17:25

    What your statements are telling you is just that "" isn't the same as null - which is true. "" is an empty string; null means that no value has been assigned.

    It might be more enlightening to try:

    System.out.println(a.length()); // 0
    System.out.println(b.length()); // error; b is not an object
    

    "" is still a string, meaning you can call its methods and get meaningful information. null is an empty variable - there's literally nothing there.

提交回复
热议问题