【发布时间】:2014-08-09 19:18:55
【问题描述】:
public static void main(String[] args) throws FileNotFoundException, IOException {
Integer a = 120;
Integer b = 120;
Integer c = 130;
Integer d = 130;
System.out.println(a==b); //true
System.out.println(c==d); //false
}
这种行为让我很困惑。谁能解释一下?
【问题讨论】:
-
不要使用 box'ed 对象,将
Integer更改为int你会看到你所期望的 -
@DmitryFucintv Java 中
==与equals的行为以及整数缓存都在许多答案中进行了多次解释。这很可能是您得到几票反对的原因。
标签: java comparison