但是,我遇到了一个错误,将其中一个更改为,它修复了该错误。==
.equals()
不好吗?什么时候应该和不应该使用它?有什么区别?==
==
测试引用相等性(它们是否是同一对象)。
.equals()
测试值相等性(它们是否包含相同的数据)。
Objects.equals() 在调用之前检查,因此您不必这样做(从 JDK7 开始可用,在 Guava 中也可用)。null
.equals()
因此,如果要测试两个字符串是否具有相同的值,则可能需要使用 。Objects.equals()
// These two have the same value
new String("test").equals("test") // --> true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
您几乎总是想使用 .在极少数情况下,当您知道您正在处理被扣留的字符串时,您可以使用 .Objects.equals()
==
从 JLS 3.10.5 开始。字符串文字:
此外,字符串文字总是引用类的同一实例。这是因为字符串文字(或者更一般地说,作为常量表达式值的字符串 (§15.28))被“扣留”,以便使用该方法共享唯一实例。
String
String.intern
类似的例子也可以在JLS 3.10.5-1中找到。
忽略大小写的 String.equalsIgnoreCase() 值相等。但是请注意,此方法在各种与区域设置相关的情况下可能会产生意外结果,请参阅此问题。
String.contentEquals() 将 的内容与 any(从 Java 1.5 开始可用)的内容进行比较。使您不必在进行相等比较之前将 StringBuffer 等转换为字符串,但将空检查留给您。String
CharSequence
==
测试对象引用,测试字符串值。.equals()
有时看起来好像比较值,因为Java做了一些幕后工作来确保相同的内联字符串实际上是同一个对象。==
例如:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
但要小心空值!
==
可以很好地处理字符串,但从空字符串调用将导致异常:null
.equals()
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
因此,如果您知道这可能是空的,请通过写来告诉读者fooString1
System.out.print(fooString1 != null && fooString1.equals("bar"));
以下内容较短,但检查 null 不太明显:
System.out.print("bar".equals(fooString1)); // "bar" is never null
System.out.print(Objects.equals(fooString1, "bar")); // Java 7 required
==
比较对象引用。
.equals()
比较字符串值。
有时会产生比较字符串值的错觉,如以下情况:==
String a="Test";
String b="Test";
if(a==b) ===> true
这是因为当您创建任何字符串文本时,JVM 首先在字符串池中搜索该文本,如果找到匹配项,则将为新字符串提供相同的引用。因此,我们得到:
(a==b) ===> 真
String Pool
b -----------------> "test" <-----------------a
但是,在以下情况下失败:==
String a="test";
String b=new String("test");
if (a==b) ===> false
在这种情况下,对于语句,将在堆上创建新的字符串,并且该引用将提供给 ,因此将在堆上给出引用,而不是在字符串池中。new String("test")
b
b
现在指向字符串池中的字符串,同时指向堆上的字符串。因此,我们得到:a
b
if(a==b) ===> false。
String Pool
"test" <-------------------- a
Heap
"test" <-------------------- b
虽然总是比较字符串的值,所以它在两种情况下都给出 true:.equals()
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true
所以使用总是更好。.equals()
模板简介:该模板名称为【==和equals()有什么区别?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【Java】栏目查找您需要的精美模板。