如果语句与字符串比较失败
我真的不知道为什么下面的 if 语句没有执行:
if (s == "/quit")
{
System.out.println("quitted");
}
下面是整个类。
这可能是一个非常愚蠢的逻辑问题,但我一直在这里抓狂,无法弄清楚这一点。
感谢您的关注:)
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");
}
}
}
}
}
I really don't know why the if statement below is not executing:
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");
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在您的示例中,您正在比较字符串对象,而不是它们的内容。
你的比较应该是:
或者如果
s
字符串无效不介意/或者你真的不喜欢 NPE:In your example you are comparing the string objects, not their content.
Your comparison should be :
Or if
s
string nullity doesn't mind / or you really don't like NPEs:要比较字符串是否相等,不要使用 ==。 == 运算符检查两个对象是否完全相同:
在 Java 中,有很多字符串比较。
To compare Strings for equality, don't use ==. The == operator checks to see if two objects are exactly the same object:
In Java there are many string comparisons.
java中的
String
是对象,因此当与==
进行比较时,您比较的是引用,而不是值。 正确的方法是使用equals()
。不过,有一个办法。 如果要使用
==
运算符比较String
对象,可以利用 JVM 处理字符串的方式。 例如:b
将为true
。 为什么?因为 JVM 有一个
String
常量表。 因此,每当您使用字符串文字(引号"
)时,虚拟机都会返回相同对象,因此==
返回true< 。
通过使用
intern()
方法。它返回与该表中当前字符串值相对应的对象(或将其放在那里,如果不是)。String
s in java are objects, so when comparing with==
, you are comparing references, rather than values. The correct way is to useequals()
.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:b
would betrue
. Why?Because the JVM has a table of
String
constants. So whenever you use string literals (quotes"
), the virtual machine returns the same objects, and therefore==
returnstrue
.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:您不应该使用 == 进行字符串比较。 该运算符只会检查它是否是相同的实例,而不是相同的值。 使用 .equals 方法检查相同的值。
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.
您可以使用
或
后者制作 字典序比较,如果两个字符串相同则返回0。
You can use
or
The latter makes a lexicographic comparison, and will return 0 if the two strings are the same.
如果您使用 C++ 和 Java 进行编码,最好记住在 C++ 中,字符串类重载了 == 运算符。 但在 Java 中却并非如此。 您需要使用
equals()
或equalsIgnoreCase()
来实现这一点。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()
orequalsIgnoreCase()
for that.