弦乐似乎并不等同于Java的Andr​​oid上,尽管它们打印相同 [英] Strings don't seem to be equal in Java on Android, even though they print the same

查看:294
本文介绍了弦乐似乎并不等同于Java的Andr​​oid上,尽管它们打印相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我相当困惑的一个问题。我有code以下线在我的Andr​​oid应用程序:

I've got a problem that I'm rather confused about. I have the following lines of code in my android application:

System.out.println(CurrentNode.getNodeName().toString());
if (CurrentNode.getNodeName().toString() == "start") {
    System.out.println("Yes it does!");
} else {
    System.out.println("No it doesnt");
}

当我看到第一个println语句,它在LogCat中显示为开始(不带引号明显)的输出。但是当if语句执行它转到else语句,并打印不,没有关系。

When I look at the output of the first println statement it shows up in LogCat as "start" (without the quotes obviously). But then when the if statement executes it goes to the else statement and prints "No it doesn't".

我想知道,如果节点的名称可能有某种在其非打印字符,所以我检查从getNodeName()传来的字符串的长度,这是5个字符,如你所愿。

I wondered if the name of the node might have some kind of non-printing character in it, so I've checked the length of the string coming from getNodeName() and it is 5 characters long, as you would expect.

有没有人有任何想法是怎么回事?

Has anyone got any idea what's going on here?

推荐答案

使用字符串的<一个href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#equals%28java.lang.Object%29">equals方法来比较字符串。该 == 运营商将只比较对象引用。

Use String's equals method to compare Strings. The == operator will just compare object references.

if ( CurrentNode.getNodeName().toString().equals("start") ) {
   ...

这篇关于弦乐似乎并不等同于Java的Andr​​oid上,尽管它们打印相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆