这段代码将一个字符串分离为令牌,并将它们存储在一个字符串数组中,然后将一个变量与第一个home…为什么它不工作?

public static void main(String...aArguments) throws IOException {

    String usuario = "Jorman";
    String password = "14988611";

    String strDatos = "Jorman 14988611";
    StringTokenizer tokens = new StringTokenizer(strDatos, " ");
    int nDatos = tokens.countTokens();
    String[] datos = new String[nDatos];
    int i = 0;

    while (tokens.hasMoreTokens()) {
        String str = tokens.nextToken();
        datos[i] = str;
        i++;
    }

    //System.out.println (usuario);

    if ((datos[0] == usuario)) {
        System.out.println("WORKING");
    }
}

当前回答

==测试引用是否相等。

.equals()测试值是否相等。

因此,如果你真的想测试两个字符串是否具有相同的值,你应该使用.equals()(除非在少数情况下,你可以保证具有相同值的两个字符串将由相同的对象表示,例如:字符串实习)。

==用于测试两个字符串是否是同一个Object。

// 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 

// concatenation of string literals happens at compile time resulting in same objects
"test" == "te" + "st"  ==> true

// but .substring() is invoked at runtime, generating distinct objects
"test" == "!test".substring(1) ==> false

值得注意的是,==比equals()更便宜(一个指针比较而不是一个循环),因此,在适用的情况下(例如,您可以保证您只处理实习字符串),它可以提供重要的性能改进。然而,这种情况很少见。

其他回答

==测试引用是否相等。

.equals()测试值是否相等。

因此,如果你真的想测试两个字符串是否具有相同的值,你应该使用.equals()(除非在少数情况下,你可以保证具有相同值的两个字符串将由相同的对象表示,例如:字符串实习)。

==用于测试两个字符串是否是同一个Object。

// 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 

// concatenation of string literals happens at compile time resulting in same objects
"test" == "te" + "st"  ==> true

// but .substring() is invoked at runtime, generating distinct objects
"test" == "!test".substring(1) ==> false

值得注意的是,==比equals()更便宜(一个指针比较而不是一个循环),因此,在适用的情况下(例如,您可以保证您只处理实习字符串),它可以提供重要的性能改进。然而,这种情况很少见。

使用字符串。equals(对象其他)函数用于比较字符串,而不是==操作符。

该函数检查字符串的实际内容,==操作符检查对对象的引用是否相等。请注意,字符串常量通常是“内嵌”的,这样两个具有相同值的常量实际上可以用==进行比较,但最好不要依赖于此。

if (usuario.equals(datos[0])) {
    ...
}

注意:比较是在'usuario'上完成的,因为这在你的代码中是保证非空的,尽管你仍然应该检查你实际上在datos数组中有一些令牌,否则你会得到一个数组越界异常。

满足Jorman

Jorman是一个成功的商人,他有两套房子。

但其他人并不知道。

还是那个乔曼吗?

当你问麦迪逊街或伯克街的邻居时,他们唯一能说的就是:

仅从住所来看,很难确定是同一个人。因为他们是两个不同的地址,所以很自然地假设他们是两个不同的人。

这就是运算符==的行为。它会说datos[0]==usuario是假的,因为它只比较地址。

一个救援的调查员

如果我们派个调查员去呢?我们知道是同一个乔曼,但我们需要证明。我们的警探会仔细检查身体的各个方面。通过彻底的调查,代理人将能够得出结论,是否是同一个人。让我们看看它在Java术语中的情况。

下面是String的equals()方法的源代码:

它逐个字符地比较字符串,以便得出它们确实相等的结论。

这就是String =方法的行为。因此datos[0].equals(通常)将返回true,因为它执行逻辑比较。

有人在更高的帖子上说==用于int和检查null。 它也可以用来检查布尔运算和字符类型。

但是要非常小心,仔细检查你使用的是char类型而不是String类型。 例如

    String strType = "a";
    char charType = 'a';

对于字符串,您将检查 这是正确的

    if(strType.equals("a")
        do something

but

    if(charType.equals('a')
        do something else

是不正确的,你需要做下面的事情

    if(charType == 'a')
         do something else

你应该使用string equals来比较两个相等的字符串,而不是operator ==,它只是比较引用。