Difference between null and empty (“”) Java String

前端 未结 22 1699
再見小時候
再見小時候 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:42

    String s = "";
    s.length();
    
    String s = null;
    s.length();
    

    A reference to an empty string "" points to an object in the heap - so you can call methods on it.

    But a reference pointing to null has no object to point in the heap and thus you'll get a NullPointerException.

提交回复
热议问题