foo="bar";如果(foo == "bar"){ doX(); }else{ "But this ALWAYS runs" } 为什么?

foo="bar"; if(foo == "bar"){ doX(); }else{ "But this ALWAYS runs" } Why?

我的 class 实现了一个非常简单的 RPN 计算器原型。

以下构造无效。为什么?我在这里做错了什么?

public boolean executeCommand(String command) {
    if(command == "+")      { add();          return true; }else
    if(command == "-")      { subtrair();     return true; }else
    if(command == "*")      { multiplicar();  return true; }else
    if(command == "/")      { dividir();      return true; }else
        {
            System.out.println("The command does not exist.");
            return false;
        }
}

输出总是,无论字符串包含什么,

The command does not exist.

为什么?我真的不明白!如果有人能解释一下,我将不胜感激!


更详细

有问题的方法是:

public boolean executeCommand(String command) {
    Scanner str = new Scanner(command);

    if (str.hasNextDouble()) {
        dataStack.push(str.nextDouble());
        return true;
    } else {

        System.out.format(" DEBUG: command: %s$%n", command);

        if(command == "+")      { add();          return true; }else
        if(command == "-")      { subtract();     return true; }else
        if(command == "*")      { multiply();     return true; }else
        if(command == "/")      { divide();       return true; }else
        if(command == ".")      { print();        return true; }else
        if(command == ".s")     { showStack();    return true; }else
        if(command == "exit")   { exit();         return true; }else
            {
                System.out.println("The command does not exist.");
                return false;
            }
    }
}

对于任何输入我扔给它,(当然除了数字,结果是:

 DEBUG: command: [COMMAND HERE]$
The command does not exist.

源代码

我删除了一些不相关的源代码; (即一些方法,包名)但它仍然是可编译和可运行的:

import java.util.Scanner;
import java.util.LinkedList;

public class RPNCalculator {

    public static void main(String[] args) {
        RPNCalculator calc = new RPNCalculator();
        calc.startInteractiveMode();
    }

    protected Scanner scanInput;
    public LinkedList<Double> dataStack;
    protected boolean interactiveModeEnabled;

    public RPNCalculator() {
        scanInput = new Scanner(System.in).useDelimiter("\s+");
        dataStack = new LinkedList<Double>();
    }

    public String getCommand() {
        return scanInput.next();
    }

    public boolean executeCommand(String command) {
        Scanner str = new Scanner(command);

        if (str.hasNextDouble()) {
            dataStack.push(str.nextDouble());
            return true;
        } else {

            System.out.format(" DEBUG: command: %s$%n", command);

            if(command == "+")   { ommitedOp("add");      return true; }else
            if(command == "-")   { ommitedOp("subtract"); return true; }else
            if(command == "*")   { ommitedOp("multiply"); return true; }else
            if(command == "/")   { ommitedOp("divide");   return true; }else
            if(command == ".")   { ommitedOp("print");    return true; }else
            if(command == ".s")  { ommitedOp("showStack");return true; }else
            if(command == "exit"){ ommitedOp("exit");     return true; }else
                {
                    System.out.println("The command does not exist.");
                    return false;
                }
        }
    }

    public void startInteractiveMode() {
        interactiveModeEnabled = true;

        while (interactiveModeEnabled) {
            String command = getCommand();
            executeCommand(command);
        }
    }

    public void ommitedOp(String method){
        System.out.println("Command exists!");
    }
}

...我想我明白了。谢谢,Stack Overflow 的类似问题!

问题在于我如何尝试使用 == 运算符,它只比较指针而不比较 Strings 本身:

In Java, you must use equals() for comparing equality between Strings. == tests for identity, a different concept.

Two objects can be equal but not identical; on the other hand if two objects are identical it's implied that they're equal.

Two objects are identical if they physically point to the same address in memory, whereas two objects are equal if they have the same value, as defined by the programmer in the equals() method. In general, you're more interested in finding out if two objects are equal.

— answered May 10 '12 at 14:11 by Óscar López

现在,让我们在发帖之前测试一下这个理论,以免自己出洋相,不必要地浪费别人的时间……确认。


因此,解决方案是使用 command.equals("COMMAND NAME") 而不是 command == "COMMAND NAME",像这样:

public boolean executeCommand(String command) {
    if(command.equals("+"))      { add();          return true; }else
    if(command.equals("-"))      { subtrair();     return true; }else
    if(command.equals("*"))      { multiplicar();  return true; }else
    if(command.equals("/"))      { dividir();      return true; }else
        {
            System.out.println("The command does not exist.");
            return false;
        }
}
command == "+"   // always checks for reference and it will be never same.

改为使用下面的

command.equals("=")