首页 > 代码库 > Difference between null and empty String
Difference between null and empty String
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。