【发布时间】:2014-12-27 05:01:50
【问题描述】:
什么时候使用比较运算符是安全的,例如==, >=,
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
我知道 == 对引用很危险,您应该使用 .equals 代替,但是其他运算符呢?例如
int x = 7;
int y = 7;
Integer a = new Integer(7);
Integer b = new Integer(7);
System.out.println(x == y);
System.out.println(a == b);
System.out.println(a.equals(b));
System.out.println(a <= b);
打印
true
false // Why you should use .equals
true
true // Seemed dangerous but it worked?
是否可以使用除双等号以外的任何内容(所以 >、= 和
【问题讨论】:
标签: java reference integer comparison-operators