对 Java 字符串使用 '==' 而不是 .equals

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3689952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:01:34  来源:igfitidea点击:

Using '==' instead of .equals for Java strings

javaequals

提问by Aillyn

Possible Duplicate:
What makes reference comparison (==) work for some strings in Java?

可能的重复:
是什么使引用比较 (==) 对 Java 中的某些字符串起作用?

I know this has been asked before, but in spite of recommendations to use .equals()instead of the ==comparison operator, I found that ==works all the time:

我知道之前有人问过这个问题,但尽管建议使用.equals()而不是==比较运算符,我发现它一直==有效:

String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2); // true

Can anyone give me an example of the ==operator failing?

谁能给我一个==操作员失败的例子?

回答by zneak

This is because you're lucky. The ==operator in Java checks for reference equality: it returns true if the pointers are the same. It does not check for contents equality. Identical strings found at compile-time are collapsed into a single Stringinstance, so it works with Stringliterals, but not with strings generated at runtime.

这是因为你很幸运。==Java 中的运算符检查引用相等性:如果指针相同,则返回 true。它不检查内容是否相等。在编译时找到的相同字符串被折叠到一个String实例中,因此它适用于String文字,但不适用于运行时生成的字符串。

For instance, "Foo" == "Foo"might work, but "Foo" == new String("Foo")won't, because new String("Foo")creates a new Stringinstance, and breaks any possible pointer equality.

例如,"Foo" == "Foo"可能会工作,但"Foo" == new String("Foo")不会,因为new String("Foo")创建了一个新String实例,并破坏了任何可能的指针相等性。

More importantly, mostStringsyou deal with in a real-world program are runtime-generated. User input in text boxes is runtime-generated. Messages received through a socket are runtime-generated. Stuff read from a file is runtime-generated. So it's very important that you use the equalsmethod, and not the ==operator, if you want to check for contents equality.

更重要的是,您在现实世界中处理的大多数Strings程序都是运行时生成的。文本框中的用户输入是运行时生成的。通过套接字接收的消息是运行时生成的。从文件中读取的内容是运行时生成的。因此,如果要检查内容是否相等,使用equals方法而不是==运算符非常重要。

回答by Prasoon Saurav

Can anyone give me an example of the == operator failing?

谁能给我一个 == 操作符失败的例子?

Example 1:

示例 1:

String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // false

Example 2:

示例 2:

Integer a=1000,b=1000;
System.out.println(a == b); // false

回答by Javid Jamae

When you do this, you are actually creating string literals:

当你这样做时,你实际上是在创建字符串文字

String s1 = "Hello";
String s2 = "Hello";

The compiler finds identical string literals and then optimizes by keeping one instance in the heap and making all the variables in the stack point to it. So doing an ==will return true because they point to the same memory address.

编译器找到相同的字符串文字,然后通过在堆中保留一个实例并使堆栈中的所有变量指向它来进行优化。因此,执行 an==将返回 true,因为它们指向相同的内存地址。

When you do this, you are creating string objects:

当你这样做时,你正在创建字符串对象

String s1 = new String("Hello");
String s2 = new String("Hello");

The instantiation will create unique space on the heap for each of these and the stack variables will point to those separate locations. Thus, these will be equal using .equals()because their values are the same, but they will not be equal using ==because they are different objects in the heap memory space.

实例化将在堆上为每一个创建独特的空间,堆栈变量将指向这些单独的位置。因此,它们将相等 using.equals()因为它们的值相同,但它们不会相等 using==因为它们是堆内存空间中的不同对象。

回答by Stephen C

Seasoned Java developers rarely if ever use new String(String), but the problem arises in other cases as well. For example:

经验丰富的 Java 开发人员很少使用new String(String),但在其他情况下也会出现问题。例如:

String hello = "Hello"
String hell = hello.substring(0, 4);
System.err.println("Hell" == hell);  // should print "false".

(Most String instances in a real-world applications are formed either by taking a substring of some other String, or by constructing it from an array of characters. Very few applications will only use String instances created as literals.)

(现实世界应用程序中的大多数 String 实例都是通过采用其他 String 的子字符串或从字符数组构造它来形成的。很少有应用程序仅使用作为文字创建的 String 实例。)