【发布时间】:2017-12-18 12:44:30
【问题描述】:
在 Java 中 Boolean.TRUE 方法有以下实现
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
现在:
System.out.println(new Boolean(true) == new Boolean(true));
System.out.println(Boolean.TRUE == Boolean.TRUE);
打印:
false
true
为什么?!
【问题讨论】:
-
一个
static字段被初始化一次,而不是在每次访问时。 -
new Boolean(true) == new Boolean(true)正在比较对象,而不是它们的内容。 -
@user109447 - 你试过我的解决方案了吗?如果有效,请接受答案并投票!