即使条件正确,if条件也不会执行

问题描述:

我有一段简单的代码可以验证用户名和密码.

I have a simple piece of code to validate a username and a password.

    public boolean isValid(String u, String p) {
    if (u=="admin" && p=="password1") {
        return true;
    } else if (u=="user" && p=="password2") {
        return true;
    } else {
        return false;
    }

}

我尝试调试它,当它运行时,u的值为"admin",p的值为"password1",但它只是跳过了第一个条件.我一定做错了什么,但我不知道该怎么办.

I've tried debugging it, and when it runs, u has the value "admin" and p has the value "password1", but it just skips the first condition. I must have done something wrong, but I can't figure out what.

== 不应用于字符串比较.改用 equals().

== should not be used for String comparison. Use equals() instead.