Java Method.getName()与string不匹配

Max*_*son 0 java methods matching

我已经用几种不同的方法对它进行了测试.我比较的字符串s与日志文件中显示的字符串完全相同.撇号是为了确保没有空格.有谁知道发生了什么?

import java.lang.reflect.Method;
import android.util.Log;

public class Button {
    public Button () {
        for(Method m1:MyOtherClass.class.getMethods()) {
        String s = m1.getName();
            if(s == "Update") {
                Log.i("result","true");
            }
            Log.i("test", "'" + s + "'");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Den*_*eng 5

你的问题是:

if(s == "Update")
Run Code Online (Sandbox Code Playgroud)

替换为

if (s.equals("Update"))
Run Code Online (Sandbox Code Playgroud)

==在处理对象(如String)时比较引用,而不是内容/值.