java 如何比较if-else语句中的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7677627/
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 20:56:17  来源:igfitidea点击:

How to compare Strings in if-else statements?

javajspif-statement

提问by Gary Deuces Rozanski

I wonder if anyone could give me a pointer with some pre-population in JSP:

我想知道是否有人可以给我一个在 JSP 中预先填充的指针:

So I am attempting to pre-populate a checkbox depending on the value in the customers database record. The checkbox would be checked if there is a 1 in the field, the other option would be a 0 - where the checkbox should be empty.

所以我试图根据客户数据库记录中的值预先填充一个复选框。如果字段中有 1,则复选框将被选中,另一个选项将是 0 - 复选框应为空。

I am currently using this code within the input:

我目前在输入中使用此代码:

<%if (UPDATEDPREFERENCES != null)
{ 
    if (ISNEWSLETTER != "0") {
        out.println("checked");
    } 
}
%>

so it would be:

所以它会是:

<input type='checkbox' class="newsletter" name='temp_ISNEWSLETTER_FIELD' id="temp_ISNEWSLETTER_FIELD" <%if (UPDATEDPREFERENCES != null)
{ 
    if (ISNEWSLETTER != "0") {
        out.println("checked");
    } 
}
%>/>

As it stands, it pre-populates fine. However, it also populates if the field is 0. If I change it to read:

就目前而言,它预填充得很好。但是,如果该字段为 0,它也会填充。如果我将其更改为:

if (ISNEWSLETTER == "1")

then the checkbox is empty whether it's a 1 or 0.

然后复选框是空的,无论是 1 还是 0。

Am I missing something obvious? I am not a developer, I am in NYC and my support is in Belgium - they've all left for the day.

我错过了一些明显的东西吗?我不是开发人员,我在纽约,我的支持在比利时 - 他们都离开了。

回答by BalusC

Stringsare objects, not primitives. Use equals(), not ==/!=. The ==/!=won't check if they contain the same value, but if they point to the same object reference. The equals()will check if they contain the same value.

字符串对象,而不是原语。使用equals(),而不是==/ !=。该==/!=不会检查它们是否包含相同的值,但如果它们指向同一个对象的引用。该equals()会检查它们是否包含相同的值。

So, instead of if (ISNEWSLETTER != "0")you should use

所以,而不是if (ISNEWSLETTER != "0")你应该使用

if (!ISNEWSLETTER.equals("0"))

and instead of if (ISNEWSLETTER == "1")you should use

而不是if (ISNEWSLETTER == "1")你应该使用

if (ISNEWSLETTER.equals("1"))

This problem is not related to JSP. You would have exactly the same problem when doing so in a normal Java class (where this kind of code actuallybelongs).

此问题与 JSP 无关。在普通 Java 类(这种代码实际上属于其中)中执行此操作时,您会遇到完全相同的问题。

As an alternative, since they represent numbers, you could also just convert ISNEWSLETTERto an int. It's a primitive, so you should be able to use ==/!=the usual way. You can if necessary convert a Stringto an intusing Integer#parseInt(). Or, even more, if it actually represents a boolean state (it can only be trueor false), then you should rather use booleaninstead. You can just use it straight in the ifcondition without any need for comparisons.

作为替代方案,由于它们代表数字,您也可以将其转换ISNEWSLETTERint. 这是一个原语,所以你应该能够使用==/!=通常的方式。如有必要,您可以将 a 转换Stringintusing Integer#parseInt()。或者,甚至更多,如果它实际上表示一个布尔状态(它只能是truefalse),那么您应该boolean改为使用。您可以直接在if条件下使用它,而无需进行任何比较。