Java if()语句无效

3
当我运行一些代码(稍后会展示),我告诉它检查字符串是否等于“1”,如果是,输出“It's 1!”,否则输出该字符串。
代码: ```python if string == "1": print("It's 1!") else: print(string) ```
double shirt3 = Math.random() * 9;
String shirt2 = Double.toString(shirt3);
char shirt1 = shirt2.charAt(0);
String shirt = Character.toString(shirt1);

if(shirt == "1") {
    System.out.println("It's 1!");
} else {
    System.out.println(shirt);
}

输出:

7
4
8
1
7
1
7
7
6
0


http://stackoverflow.com/questions/10053033/my-if-statement-doesnt-work/10053041#10053041 - Samir Mangroliya
可能是重复的问题,参考 Java String.equals versus == - trutheality
6个回答

9

您需要使用

if (shirt.equals("1"))
    ...

这将比较String对象的实际内容,而不是它们的标识符。

5
您犯了Java初学者的错误,使用 == 来测试字符串相等性。请改用equals方法。
更长的解释是,Java中的 == 运算符测试两个对象引用是否相同;即它测试左操作数和右操作数是否为同一对象。但你有两个包含相同字符序列的不同字符串。它们是“相等”的字符串,但不是同一个对象。
作为一般准则,您应始终使用equals来比较字符串。
(在某些情况下, == 将起作用,但您真的需要了解自己在做什么才能确定。在绝大多数用例中,这根本不值得努力/风险。)

4

在Java中检查两个字符串是否相同,使用.equals()方法:

"1" == new String("1") //returns false
"1".equals(new String("1")) //returns true

编辑:添加了新的String("1")以确保我们正在处理一个新字符串。


5
实际上,由于字符串文字被内部化,所以 "1" == "1" 将返回 true。但是,如果这两个 "1" 中的任何一个不是编译时常量,那么就会出现问题。 - cHao

3
除了在Java中使用equals方法来比较字符串之外,还有其他一些事实。
// it't allways a good idea to use constant.equals(...)
// to avoid possible NullPointerExceptions
if ("1".equals(shirt))
    ...

在您的情况下,您不需要将您的字符转换为字符串,您也可以比较单个字符。这样做可以避免创建额外的字符串对象,并且您不必处理equals方法。
if (shirt1 == '1')
        ...

在编程中,将以下内容从英语翻译成中文:+1 为提供最佳解决方案:完全绕过重新字符串化和字符串问题。 :) - cHao
2
@cHao - 这是最佳解决方案,但更重要的是OP理解使用==与String是错误的原因。 - Stephen C

2

要比较字符串,请使用equals。

if(shirt.equals("1"))
     System.out.println("It's 1!");
    }else{
        System.out.println(shirt);
    }

1
一个更普遍的规则是尽量不要让代码变得比必要复杂。
int shirt = (int)(Math.random() * 10); // shirt numbers from 0 to 9.
if(shirt == 1) 
    System.out.println("It's 1!");
else 
    System.out.println(shirt);

这说明==可以用于比较原始类型。它也可以用于比较引用,但不能用于比较对象的内容。
Double d = 0.1;
Double e = 0.1;
System.out.println("(Double) 0.1 == (Double) 0.1 is " + (d == e));

double x = 0.1;
double y = 0.1;
System.out.println("0.1 == 0.1 is " + (x == y));

打印

(Double) 0.1 == (Double) 0.1 is false
0.1 == 0.1 is true

这表明,在比较Double时,与字符串一样,对象==不会比较内容。

在使用缓存时存在一个混淆,例如字符串文字。这意味着出于性能原因,在不同位置引用的值实际上使用相同的对象。

Integer d = 10;
Integer e = 10;
System.out.println("(Integer) 10 == (Integer) 10 is " + (d == e));

int x = 10;
int y = 10;
System.out.println("10 == 10 is " + (x == y));

输出

(Integer) 10 == (Integer) 10 is true
10 == 10 is true

第一个例子有效是因为Java 5.0+使用了小整数的缓存。(小整数的大小取决于命令行参数:})
Integer d = -129;
Integer e = -129;
System.out.println("(Integer) -129 == (Integer) -129 is " + (d == e));

int x = -129;
int y = -129;
System.out.println("-129 == -129 is " + (x == y));

打印

(Integer) -129 == (Integer) -129 is false
-129 == -129 is true

关于字符串,使用字符串字面量缓存。此外,编译器将简化常量表达式,因此以不同方式编写的字符串可以相同。
final int one = 1;
int oneB = 1;
String a = "1";
String b = "" + 1;
String c = "" + one;
String d = "" + oneB;

System.out.println(a == b);
System.out.println(a == c);
System.out.println(a == d);

打印

true
true
false

每个字符串的内容是相同的,但oneB不是一个常量,因此表达式在运行时被评估并产生不同的字符串。
在我看来:Java试图隐藏开发人员的细节,最好让==调用equals,而如果您真的想比较实际引用,则可以使用操作符===

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接