null和empty(“"”)Java String之间的区别 [英] Difference between null and empty ("") Java String

查看:150
本文介绍了null和empty(“"”)Java String之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

null (空字符串)有什么区别?



我写了一些简单的代码:

  String a =; 
String b = null;

System.out.println(a == b); // false
System.out.println(a.equals(b)); // false

两个语句都返回 false 。看来,我无法找到它们之间的实际差异。

解决方案

是一个实际的字符串,尽管空的。



null,但是,意味着String变量指向任何内容。



a == b 返回false,因为和null不占用内存中的相同空间 - 换句话说,它们的变量不指向相同的对象。



a.equals(b)返回false,因为显然不等于null。



不同之处在于,因为是一个实际的字符串,你仍然可以在其上调用方法或函数,如



a.length()



a.substring(0,1)



依此类推。



如果String等于null,就像b一样,Java会抛出 NullPointerException 如果您尝试调用,请说:



b.length()






如果是您想知道的差异是==与等于,这就是:



==比较参考,就像我去了

  String a = new String(); 
String b = new String();
System.out.println(a == b);

这会输出错误,因为我分配了两个不同的对象,而a和b指向不同的对象。 / p>

但是,在这种情况下, a.equals(b)将返回true,因为等于 for Strings将返回true 当且仅当参数String不为null并且表示相同的字符序列时。



但是,请注意Java确实有字符串的特殊情况。

 字符串a =abc; 
String b =abc;
System.out.println(a == b);

你会认为输出是 false ,因为它应该分配两个不同的字符串。实际上,Java将实习生文字字符串(那些在我们的例子中被初始化为a和b)。所以要小心,因为这可能会对==如何工作产生误报。


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

I have written some simple code:

String a = "";
String b = null;

System.out.println(a == b); // false
System.out.println(a.equals(b)); // false

Both statements return false. It seems, I am not able to find what is the actual difference between them.

解决方案

"" is an actual string, albeit an empty one.

null, however, means that the String variable points to nothing.

a==b returns false because "" and null do not occupy the same space in memory--in other words, their variables don't point to the same objects.

a.equals(b) returns false because "" does not equal null, obviously.

The difference is though that since "" is an actual string, you can still invoke methods or functions on it like

a.length()

a.substring(0, 1)

and so on.

If the String equals null, like b, Java would throw a NullPointerException if you tried invoking, say:

b.length()


If the difference you are wondering about is == versus equals, it's this:

== compares references, like if I went

String a = new String("");
String b = new String("");
System.out.println(a==b);

That would output false because I allocated two different objects, and a and b point to different objects.

However, a.equals(b) in this case would return true, because equals for Strings will return true if and only if the argument String is not null and represents the same sequence of characters.

Be warned, though, that Java does have a special case for Strings.

String a = "abc";
String b = "abc";
System.out.println(a==b);

You would think that the output would be false, since it should allocate two different Strings. Actually, Java will intern literal Strings (ones that are initialized like a and b in our example). So be careful, because that can give some false positives on how == works.

这篇关于null和empty(“"”)Java String之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆