Java 如果带有字符串比较的语句失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/658953/
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
If statement with String comparison fails
提问by
I really don't know why the if statement below is not executing:
我真的不知道为什么下面的 if 语句没有执行:
if (s == "/quit")
{
System.out.println("quitted");
}
Below is the whole class.
下面是全班。
It is probably a really stupid logic problem but I have been pulling my hair out over here not being able to figure this out.
这可能是一个非常愚蠢的逻辑问题,但我一直在这里拉我的头发无法弄清楚这一点。
Thanks for looking :)
谢谢你看:)
class TextParser extends Thread {
public void run() {
while (true) {
for(int i = 0; i < connectionList.size(); i++) {
try {
System.out.println("reading " + i);
Connection c = connectionList.elementAt(i);
Thread.sleep(200);
System.out.println("reading " + i);
String s = "";
if (c.in.ready() == true) {
s = c.in.readLine();
//System.out.println(i + "> "+ s);
if (s == "/quit") {
System.out.println("quitted");
}
if(! s.equals("")) {
for(int j = 0; j < connectionList.size(); j++) {
Connection c2 = connectionList.elementAt(j);
c2.out.println(s);
}
}
}
} catch(Exception e){
System.out.println("reading error");
}
}
}
}
}
回答by WiseTechi
In your example you are comparing the string objects, not their content.
在您的示例中,您正在比较字符串对象,而不是它们的内容。
Your comparison should be :
你的比较应该是:
if (s.equals("/quit"))
Or if s
string nullity doesn't mind / or you really don't like NPEs:
或者,如果s
字符串无效不介意/或者您真的不喜欢 NPE:
if ("/quit".equals(s))
回答by digitaljoel
You shouldn't do string comparisons with ==. That operator will only check to see if it is the same instance, not the same value. Use the .equals method to check for the same value.
你不应该用 == 进行字符串比较。该运算符只会检查它是否是相同的实例,而不是相同的值。使用 .equals 方法检查相同的值。
回答by TStamper
To compare Strings for equality, don't use ==. The ==operator checks to see if two objects are exactly the same object:
要比较字符串是否相等,请不要使用 ==。在==操作符检查是否两个对象是完全一样的对象:
In Javathere are many string comparisons.
在Java 中有很多字符串比较。
String s = "something", t = "maybe something else";
if (s == t) // Legal, but usually WRONG.
if (s.equals(t)) // RIGHT
if (s > t) // ILLEGAL
if (s.compareTo(t) > 0) // also CORRECT>
回答by littleduckie
You can use
您可以使用
if("/quit".equals(s))
...
or
或者
if("/quit".compareTo(s) == 0)
...
The latter makes a lexicographiccomparison, and will return 0 if the two strings are the same.
后者进行字典比较,如果两个字符串相同则返回0。
回答by user855
If you code in C++ as well as Java, it is better to remember that in C++, the string class has the == operator overloaded. But not so in Java. you need to use equals()
or equalsIgnoreCase()
for that.
如果您使用 C++ 和 Java 编写代码,最好记住在 C++ 中,字符串类重载了 == 运算符。但在 Java 中并非如此。你需要使用equals()
或equalsIgnoreCase()
为此。
回答by Bozho
String
s in java are objects, so when comparing with ==
, you are comparing references, rather than values. The correct way is to use equals()
.
String
java 中的 s 是对象,因此与 比较时==
,您比较的是引用,而不是值。正确的方法是使用equals()
.
However, there is a way. If you want to compare String
objects using the ==
operator, you can make use of the way the JVM copes with strings. For example:
不过,有办法。如果要String
使用==
运算符比较对象,可以利用 JVM 处理字符串的方式。例如:
String a = "aaa";
String b = "aaa";
boolean b = a == b;
b
would be true
. Why?
b
会true
。为什么?
Because the JVM has a table of String
constants. So whenever you use string literals (quotes "
), the virtual machine returns the sameobjects, and therefore ==
returns true
.
因为JVM有一个常量表String
。因此,无论何时使用字符串文字(引号"
),虚拟机都会返回相同的对象,因此==
返回true
.
You can use the same "table" even with non-literal strings by using the intern()
method. It returns the object that corresponds to the current string value from that table (or puts it there, if it is not). So:
通过使用该intern()
方法,您甚至可以对非文字字符串使用相同的“表” 。它返回与该表中的当前字符串值对应的对象(或者将它放在那里,如果不是的话)。所以:
String a = new String("aa");
String b = new String("aa");
boolean check1 = a == b; // false
boolean check1 = a.intern() == b.intern(); // true
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
因此,对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为真时,s.intern() == t.intern() 为真。