45

字符串部分是 String[6]:

[“231”、“CA-California”、“Sacramento-155328”、“aleee”、“客户服务文员”、“Alegra Keith.doc.txt”]

但是当我比较parts[0]"231"

"231" == parts[0]

上面的结果是假的,

我很困惑,所以有人能告诉我为什么吗?

4

12 回答 12

78

运算符比较对象引用,而==不是Strings 的值。

要比较Strings 的值,请使用以下String.equals方法:

"231".equals(parts[0]);

对于 Java 中的任何其他对象都是如此——比较值时,始终使用equals方法而不是使用==运算符。

equals方法是 的一部分Object,并且应该被将以一种或另一种方式进行比较的类覆盖。

于 2009-06-15T12:50:02.037 回答
15

如果字符串没有被保留,则 == 检查引用标识。利用:

 "231".equals(parts[0]);

反而。

于 2009-06-15T12:50:15.717 回答
13

==在 Java 中比较对象的地址(在这种情况下为字符串)。

你想要的是parts[0].equals("231")

于 2009-06-15T12:49:58.603 回答
11

以下打印出“真”;

String s = "231";
if(s == "231")
{
    System.out.println("true");
}
else
{
    System.out.println("false");
}

这是因为字符串不是可变的,java 会尝试尽可能多地节省空间,因此它指向相同的内存引用。

但是,以下打印出“假”:

String s = new String("231");
if(s == "231")
{
    System.out.println("true");
}
else
{
    System.out.println("false");
}

new将强制它将字符串存储在新的内存位置。

顺便说一句,您应该始终使用.equals()来比较字符串(对于这种情况)

于 2009-06-15T17:57:30.983 回答
7

使用 equals 方法:parts[0].equals("231")。== 运算符比较对象引用。

于 2009-06-15T12:50:03.363 回答
5

“==”比较对象引用,在您的情况下,“231”是与parts[0]不同的对象。

您想使用String.equals

parts[0].equals("231")
于 2009-06-15T12:50:41.893 回答
4

答案很简单:当您通过 == 运算符比较字符串时,您实际上是在比较两个不同的变量是否引用单个 String 对象。而他们没有,数组中的字符串和新创建的“231”是具有相同内容的不同字符串对象。

正确的做法是使用以下表达式:"231".equals(parts[0])or "231".equalsIgnoreCase(parts[0])。如果这些 String 对象包含相同的值,这将为您提供所需的内容并返回 true。

于 2009-06-15T12:55:48.870 回答
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;
于 2010-01-09T21:28:07.167 回答
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);
    }
}
于 2009-06-15T17:53:52.660 回答
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
于 2010-01-09T21:19:05.820 回答
1

使用 equals 方法比较对象:

String[] test = {"231", "CA-California", "Sacramento-155328", "aleee",
                 "Customer Service Clerk", "Alegra Keith.doc.txt"};

System.out.println("231".equals(test[0]));

比较 '==' 比较引用,而不是值。

于 2009-06-15T12:57:14.173 回答
0

正如许多其他人已经解释的那样,您尝试与相等运算符进行比较,但随后它将依赖 Object.equals() 而不是 String.equals()。

所以你可以通过显式调用 String.equals() 来完成这项工作,而不是编写

parts[0].equals("blahblah")

我更喜欢这样:

"blahblah".equals(parts[0])

因为它避免了测试parts[0] 的潜在无效性(但要注意parts 变量本身可能为null...)

另一种方法是使用 String.intern() :

if (parts[0].intern() == "blahblah") ...

有关详细信息,请参阅http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#intern()

于 2009-06-15T19:03:26.047 回答