java 简单的 if else 语句不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5331827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:41:32  来源:igfitidea点击:

Simple if else statement not working

javaandroidif-statement

提问by Badr Hari

I have a simple password protection. I do it like this:

我有一个简单的密码保护。我这样做:

EditText editText1 =  (EditText) findViewById(R.id.editText1);
String Password = editText1.getText().toString();
if(Password == "a"){
  Toast.makeText(getApplicationContext(), "Success" + Password, Toast.LENGTH_SHORT).show();
} else {
  Toast.makeText(getApplicationContext(), "Failure" + Password, Toast.LENGTH_SHORT).show();
}

I have edittext and button. If user is typing in "a", toast should say success. But it is always saying failure. I don't understand what is wrong in my code...

我有编辑文本和按钮。如果用户输入“a”,toast 应该显示成功。但它总是说失败。我不明白我的代码有什么问题......

回答by Jon Skeet

In Java, using ==for non-primitive expressions will always compare object references. You're asking whether Passwordrefers to the exact same object as the string literal "a".

在 Java 中,==用于非原始表达式将始终比较对象引用。您在问是否Password与字符串文字“a”引用完全相同的对象。

Use either:

使用:

if (Password.equals("a"))

or

或者

if ("a".equals(Password))

These will call the String.equals(Object)override, which determines whether two references refer to equalString objects - i.e. the same logical sequence of characters.

这些将调用String.equals(Object)覆盖,它确定两个引用是否引用相等的String 对象——即相同的字符逻辑序列。

The former will throw an exception if Passwordis null; the latter won't. Don't treat this as a suggestion to always use the latter form - if Passwordshouldn'tbe null, then an exception may well be better than continuing in an unexpected state.

前者如果Password为空会抛出异常;后者不会。不要将此视为始终使用后一种形式的建议 - 如果Password不应为 null,则异常可能比在意外状态下继续要好。

I'd also encourage you to be consistent with your variable names - typically local variables are camelCased, so you'd use passwordinstead of Password.

我也建议你与你的变量名一致的-通常是局部变量驼峰格式,所以你会使用password代替Password

回答by Mahesh

You need to use equalsmethod. Not ==for comparison of strings. So, you should be doing -

您需要使用equals方法。不==用于比较字符串。所以,你应该做——

if( Password.equals("a") )
{
    // ....
}    

string::equals reference

字符串::等于参考

回答by kjl

Try this:

试试这个:

if(Password.equals("a"))
{ 
...
}

回答by Michael Berry

==does a reference check, i.e. it checks if the two strings are physically the same object in memory. They might be (since Java optimises some strings so they are) but you should never rely on this.

==进行引用检查,即检查两个字符串在物理上是否是内存中的同一个对象。它们可能是(因为 Java 优化了一些字符串,所以它们是)但是你永远不应该依赖这个。

equals()checks whether the strings are meaningfully equal, i.e. have the same characters, and this is what you want pretty much 100% of the time.

equals()检查字符串是否有意义地相等,即具有相同的字符,这几乎 100% 都是您想要的。

回答by Edwin Buck

In Java...

在爪哇...

Object comparison --> x.equals(y)
reference comparison --> x == y