码迷,mamicode.com
首页 > 其他好文 > 详细

Difference between null and empty String

时间:2014-09-18 06:27:43      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:io   java   ar   sp   on   c   ef   r   bs   

String s1 = ""; means that the empty String is assigned to s1. In this case, s1.length() is the same as "".length(), witch will yield 0 as expected.

String s2 = null; means that (null) or "no value at all" is assigned to s2. To this one,s2.length() is the same as null.length(), witch will yield a NullPointerException as you can‘t call methods on null variables (pointers, sort of) on Java.

Also, a point, the statement

String s1;

Actually has the same effect as:

String s1 = null;

Whereas

String s1 = "";

Is, as said, a different thing.

Difference between null and empty String

标签:io   java   ar   sp   on   c   ef   r   bs   

原文地址:http://www.cnblogs.com/EdwardLiu/p/3978457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!