public class F{
...
Boolean value;
...Boolean getValue(){
return this.value
...
}
那在两个F对象o1,o2的如下比较中:o1.getValue() == o2.getValue();比较的是o1,o2
中Boolean对象的refrence还是它们内部的boolea?我认为是前者,可惜师兄不这样认为。
写个程序实验一下(见P.S。部分)。原来师兄说得很正确,尽管与我看书得来的印象有差别;请达人给解释或者总结一下吧P.S.
class F {
Boolean value;
F(Boolean aValue){
this.value=aValue;
}
Boolean getValue(){
return this.value;
// boolean getValue(){
// return (boolean)this.value;
}
}public class Test{
public static void main(String[] args){
F o1=new F(false), o2=new F(false);
System.out.println("o1==o2 " +(o1.getValue()==o2.getValue())); }
}
Output:
o1==o2 true
...
Boolean value;
...Boolean getValue(){
return this.value
...
}
那在两个F对象o1,o2的如下比较中:o1.getValue() == o2.getValue();比较的是o1,o2
中Boolean对象的refrence还是它们内部的boolea?我认为是前者,可惜师兄不这样认为。
写个程序实验一下(见P.S。部分)。原来师兄说得很正确,尽管与我看书得来的印象有差别;请达人给解释或者总结一下吧P.S.
class F {
Boolean value;
F(Boolean aValue){
this.value=aValue;
}
Boolean getValue(){
return this.value;
// boolean getValue(){
// return (boolean)this.value;
}
}public class Test{
public static void main(String[] args){
F o1=new F(false), o2=new F(false);
System.out.println("o1==o2 " +(o1.getValue()==o2.getValue())); }
}
Output:
o1==o2 true
而Boolean.value实际上是返回Boolean类中的静态变量Boolean.TRUE和Boolean.FALSE的,所以o1.value == o2.value
如果改成 F o1=new F(new Boolean(false)), o2=new F(new Boolean(false));则会打印false
Boolean.equals()
米的答案看懂了,但不知道为什么