Java foo=";“酒吧”;;如果(foo==“bar”{doX();}其他{“但这总是运行”}为什么?

Java foo=";“酒吧”;;如果(foo==“bar”{doX();}其他{“但这总是运行”}为什么?,java,string-comparison,Java,String Comparison,我的类实现了一个非常简单的RPN计算器原型 以下构造不起作用。为什么?我做错了什么 public boolean executeCommand(String command) { if(command == "+") { add(); return true; }else if(command == "-") { subtrair(); return true; }else if(command == "*") {

我的类实现了一个非常简单的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!");
    }
}
import java.util.Scanner;
导入java.util.LinkedList;
公共类RPN计算器{
公共静态void main(字符串[]args){
RPNCalculator calc=新的RPNCalculator();
计算startInteractiveMode();
}
受保护的扫描仪扫描输入;
公共链接列表数据堆栈;
受保护的布尔交互可删除;
公共RPNCalculator(){
scanInput=新扫描仪(System.in).useDelimiter(\\s+);
dataStack=newlinkedlist();
}
公共字符串getCommand(){
返回scanInput.next();
}
公共布尔值executeCommand(字符串命令){
扫描仪str=新扫描仪(命令);
if(str.hasNextDouble()){
dataStack.push(str.nextDouble());
返回true;
}否则{
System.out.format(“调试:命令:%s$%n”,命令);
if(command==“+”{ommitedOp(“add”);返回true;}else
if(command=“-”{ommitedOp(“subtract”);返回true;}else
如果(命令==“*”){ommitedOp(“乘法”);返回true;}else
if(command==“/”{ommitedOp(“divide”);返回true;}else
if(command==“){ommitedOp(“print”);返回true;}else
if(command==.s){ommitedOp(“showStack”);返回true;}else
if(command==“exit”){ommitedOp(“exit”);返回true;}else
{
System.out.println(“该命令不存在”);
返回false;
}
}
}
public void startInteractiveMode(){
interactiveModeEnabled=true;
while(交互式可删除){
String command=getCommand();
executeCommand(命令);
}
}
公共void ommitedOp(字符串方法){
System.out.println(“命令存在!”);
}
}

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

问题在于我如何尝试使用
=
操作符,它只比较指针,而不比较字符串本身:

在Java中,必须使用
equals()
来比较
String
s之间的相等性<代码>=测试身份,这是一个不同的概念

两个对象可以相等,但不能相同;另一方面,如果两个物体相同,就意味着它们相等

如果两个对象在物理上指向内存中的相同地址,则它们是相同的;而如果两个对象具有相同的值,则它们是相等的,正如程序员在
equals()
方法中定义的那样。通常,您更感兴趣的是找出两个对象是否相等

-于2012年5月10日14:11由

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


因此,解决方案是使用
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.equals("=")

我想在维基上回答这个问题,但我还不能。@Tunaki是的,我同意。这大部分是重复的。唯一重要的区别是,这是一个具体的案例,而不是一个一般的案例:我的意思是,这可能(我不知道)对其他人有用,作为更一般案例的一个例子,所以我还是把它贴出来供将来参考。
command.equals("=")