如果语句与字符串比较失败

发布于 2024-07-15 05:02:42 字数 1418 浏览 6 评论 0原文

我真的不知道为什么下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挽袖吟 2024-07-22 05:02:42

在您的示例中,您正在比较字符串对象,而不是它们的内容。

你的比较应该是:

if (s.equals("/quit"))

或者如果 s 字符串无效不介意/或者你真的不喜欢 NPE:

if ("/quit".equals(s))

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:

if ("/quit".equals(s))
淡笑忘祈一世凡恋 2024-07-22 05:02:42

要比较字符串是否相等,不要使用 ==== 运算符检查两个对象是否完全相同:

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>

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.

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>
£噩梦荏苒 2024-07-22 05:02:42

java中的String是对象,因此当与==进行比较时,您比较的是引用,而不是值。 正确的方法是使用equals()

不过,有一个办法。 如果要使用 == 运算符比较 String 对象,可以利用 JVM 处理字符串的方式。 例如:

String a = "aaa";
String b = "aaa";
boolean b = a == b;

b 将为 true。 为什么?

因为 JVM 有一个 String 常量表。 因此,每当您使用字符串文字(引号 ")时,虚拟机都会返回相同对象,因此 == 返回 true< 。

通过使用 intern() 方法。它返回与该表中当前字符串值相对应的对象(或将其放在那里,如果不是)。

String a = new String("aa");
String b = new String("aa");
boolean check1 = a == b; // false
boolean check1 = a.intern() == b.intern(); // true

因此,对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。

Strings in java are objects, so when comparing with ==, you are comparing references, rather than values. The correct way is to use 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 a = "aaa";
String b = "aaa";
boolean b = a == b;

b would be true. 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 == returns 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:

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.

拔了角的鹿 2024-07-22 05:02:42

您不应该使用 == 进行字符串比较。 该运算符只会检查它是否是相同的实例,而不是相同的值。 使用 .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.

等往事风中吹 2024-07-22 05:02:42

您可以使用

if("/quit".equals(s))
   ...

if("/quit".compareTo(s) == 0) 
    ...

后者制作 字典序比较,如果两个字符串相同则返回0。

You can use

if("/quit".equals(s))
   ...

or

if("/quit".compareTo(s) == 0) 
    ...

The latter makes a lexicographic comparison, and will return 0 if the two strings are the same.

剩一世无双 2024-07-22 05:02:42

如果您使用 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() or equalsIgnoreCase() for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文