java 为什么 == 不适用于 String?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17443201/
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
Why doesn’t == work on String?
提问by user2545642
I just started Java programming. I love it so far, but I have been stuck on this problem for a while.
我刚开始Java编程。到目前为止我喜欢它,但我已经被这个问题困住了一段时间。
When I run this code, whenever I type in “boy” it will just respond with GIRL
:
当我运行这段代码时,每当我输入“boy”时,它都会回复GIRL
:
import java.util.Scanner;
public class ifstatementgirlorboy {
public static void main(String args[]) {
System.out.println("Are you a boy or a girl?");
Scanner input = new Scanner(System.in);
String gender = input.nextLine();
if(gender=="boy") {
System.out.println("BOY");
}
else {
System.out.println("GIRL");
}
}
}
Why?
为什么?
回答by Suresh Atta
Use the String.equals(String otherString)
function to compare strings, not the ==
operator.
使用String.equals(String otherString)
函数来比较字符串,而不是==
运算符。
This is because the ==
operator only compares object references, while
the String.equals()
method compares both String
's values i.e. the sequence of characters that make up each String
.
这是因为==
运算符只比较对象引用,而该String.equals()
方法比较两者String
的值,即组成每个的字符序列String
。
equals() method from Source code of String:
public boolean equals(Object anObject) {
1013 if (this == anObject) {
1014 return true;
1015 }
1016 if (anObject instanceof String) {
1017 String anotherString = (String)anObject;
1018 int n = count;
1019 if (n == anotherString.count) {
1020 char v1[] = value;
1021 char v2[] = anotherString.value;
1022 int i = offset;
1023 int j = anotherString.offset;
1024 while (n-- != 0) {
1025 if (v1[i++] != v2[j++])
1026 return false;
1027 }
1028 return true;
1029 }
1030 }
1031 return false;
1032 }
So you should write
所以你应该写
if(gender.equals("boy")){
}
or to comapre with regardless of case
或不分大小写
if(gender.equalsIgnoreCase("boy")){
}
and for null safety
并且为了零安全
if("boy".equals(gender)){
}
Future reference:
以后的参考:
String s1 = "Hello"; // String literal
String s2 = "Hello"; // String literal
String s3 = s1; // same reference
String s4 = new String("Hello"); // String object
String s5 = new String("Hello"); // String object
Here s1 == s2 == s3 but s4 != s5
这里 s1 == s2 == s3 but s4 != s5
Where as
然而
anyOfAbove.equals(anyOtherOfAbove); //true
anyOfAbove.equals(anyOtherOfAbove); //true
回答by Kevin Bowersox
When comparing objects of type String
, you should use the equals
method instead of operator ==
. equals
will compare the values of String
objects while ==
checks to see if they are the same object in memory.
比较类型的对象时String
,应使用equals
方法而不是运算符==
。 equals
将比较String
对象的值,同时==
检查它们是否是内存中的相同对象。
So instead of:
所以而不是:
if(gender=="boy")
use
利用
if(gender.equals("boy"))
回答by bNd
Use equals
instead of ==
使用equals
代替==
if("boy".equals(gender)){
}
use equalsTo compare the value. while ==
is compare object reference.
使用equals来比较值。while==
是比较对象引用。
回答by claw
String a,b;
a==b;
here references(addresses) of both string objects are compared
这里比较两个字符串对象的引用(地址)
a.equals(b);
here contents of both strings are compared
这里比较两个字符串的内容
回答by wassup
In Java, strings are objects (String
). Variables which contain objects are references. If you compare two objects with ==
operator, true
is returned only if they are the same objects (in memory). But in your code they aren't ("boys"
is an instantialized String on the fly).
在 Java 中,字符串是对象 ( String
)。包含对象的变量是引用。如果使用==
运算符比较两个对象,true
则仅当它们是相同的对象(在内存中)时才返回。但是在您的代码中,它们不是("boys"
是动态实例化的字符串)。
There is, however, a method String.equals()
, which compares two strings and returns true if they have the same characters in the same order, not if they are the same objects.
然而,有一个方法String.equals()
,它比较两个字符串,如果它们具有相同顺序的相同字符,则返回 true,如果它们是相同的对象,则不会。
Correct code here:
正确的代码在这里:
import java.util.Scanner;
public class ifstatementgirlorboy {
public static void main(String args[]) {
System.out.println("Are you a boy or a girl?");
Scanner input = new Scanner(System.in);
String gender = input.nextLine();
if (gender.equals("boy")) {
System.out.println("BOY");
}
else {
System.out.println("GIRL");
}
}
}
You can also swap the two strings (the advantage is that it prevents NullPointerException
from being thrown if gender == null
):
您还可以交换两个字符串(优点是它可以防止NullPointerException
被抛出 if gender == null
):
"boy".equals(gender)
To ignore the case of letters in comparison, use equalsIgnoreCase()
instead.
要在比较中忽略字母的大小写,请equalsIgnoreCase()
改用。
回答by exussum
you cant compare strings like that in java, You need to use
你不能在java中比较这样的字符串,你需要使用
if (gender.equals("boy"))
for it to work. Your way is comparing the object rather than the content
让它工作。你的方式是比较对象而不是内容
回答by Brian Agnew
You're using referenceequality there. The ==
is literally comparing the 2 references. i.e. are they the same object.
你在那里使用引用相等。这==
是字面上比较 2 参考。即它们是同一个对象。
You need to use the equals()
method, which (in this case) will compare the contentsof the two strings. See herefor more info.
您需要使用equals()
方法,它(在本例中)将比较两个字符串的内容。请参阅此处了解更多信息。