字符串部分是String [6]:
["231", "CA-California", "Sacramento-155328", "aleee", "Customer Service Clerk", "Alegra Keith.doc.txt"]
但是当我将parts[0]
与"231"
进行比较时:
"231" == parts[0]
上述结果为假,
我很困惑,所以有人能告诉我为什么吗?
答案 0 :(得分:77)
==
运算符会比较对象引用,而不是String
s的值。
要比较String
s的值,请使用String.equals
方法:
"231".equals(parts[0]);
对于Java中的任何其他对象都是如此 - 在比较值时,始终使用equals
方法而不是==
运算符。
equals
方法是Object
的一部分,应该被类重写,这些类将以这种或那种方式进行比较。
答案 1 :(得分:15)
如果字符串未被实现,则==检查引用标识。使用:
"231".equals(parts[0]);
代替。
答案 2 :(得分:13)
==
比较了对象的地址(本例中是字符串)。
你想要的是parts[0].equals("231")
答案 3 :(得分:10)
以下打印出“true”;
String s = "231";
if(s == "231")
{
System.out.println("true");
}
else
{
System.out.println("false");
}
这是因为字符串不可变,java会尽量节省空间,因此它指向同一个内存引用。
但是,以下打印出“false”:
String s = new String("231");
if(s == "231")
{
System.out.println("true");
}
else
{
System.out.println("false");
}
new
将强制它将字符串存储在新的内存位置。
顺便说一句,你应该总是使用.equals()
比较字符串(对于像这样的情况)
答案 4 :(得分:7)
使用equals方法:parts [0] .equals(“231”)。 ==运算符比较对象引用。
答案 5 :(得分:5)
答案 6 :(得分:4)
答案很简单:当你通过==运算符比较字符串时,你实际上比较了两个不同的变量是否引用了一个String对象。而他们没有,数组中的字符串和新创建的“231”是具有相同内容的不同String对象。
正确的做法是使用以下表达式:"231".equals(parts[0])
或"231".equalsIgnoreCase(parts[0])
。这将为您提供所需的内容,如果这些String对象包含相同的值,则返回true。
答案 7 :(得分:2)
我认为在测试用例中表达答案可能会有所帮助:
public class String231Test extends TestCase {
private String a;
private String b;
protected void setUp() throws Exception {
a = "231";
StringBuffer sb = new StringBuffer();
sb.append("231");
b = sb.toString();
}
public void testEquals() throws Exception {
assertTrue(a.equals(b));
}
public void testIdentity() throws Exception {
assertFalse(a == b);
}
}
答案 8 :(得分:2)
您也可以使用compareTo(String)方法:
String str = "test";
if( str.compareTo("test") == 0)
//the argument string is equal to str;
else
//the argument string is not equal to str;
答案 9 :(得分:1)
使用equals方法比较对象:
String[] test = {"231", "CA-California", "Sacramento-155328", "aleee",
"Customer Service Clerk", "Alegra Keith.doc.txt"};
System.out.println("231".equals(test[0]));
比较'=='比较参考,而不是值。
答案 10 :(得分:1)
这是一个非常好的例子。带有字符串的'=='运算符在Java中可能非常棘手。
class Foo {
public static void main(String[] args) {
String a = "hello";
String b = "hello";
String c = "h";
c = c + "ello";
String operator = null;
if(a == b) {
operator = " == ";
} else {
operator = " != ";
}
System.out.println(a + operator + b);
if(a == c) {
operator = " == ";
} else {
operator = " != ";
}
System.out.println(a + operator + c);
if(a == "hello") {
operator = " == ";
} else {
operator = " != ";
}
System.out.println(a + operator + "hello");
if(c == "hello") {
operator = " == ";
} else {
operator = " != ";
}
System.out.println(c + operator + "hello");
}
}
将产生以下输出:
hello == hello
hello != hello
hello == hello
hello != hello
答案 11 :(得分:0)
正如许多其他人已经解释过的那样,你尝试与等式运算符进行比较,但它会依赖于Object.equals()而不是String.equals()。
所以你可以通过显式调用String.equals()来完成这项工作,而不是编写
parts[0].equals("blahblah")
我更喜欢这样:
"blahblah".equals(parts[0])
因为它避免了测试零件[0]的潜在无效性(但要注意零件变量本身可能为空......)
另一种方法是使用String.intern():
if (parts[0].intern() == "blahblah") ...
有关详情,请参阅http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#intern()。