简体   繁体   English

Java中布尔和布尔的不同结果

[英]Different results with Boolean and boolean in java

My code did not give the correct result and I begin to troubleshoot and discovered a strange error, can someone explain this to me. 我的代码未给出正确的结果,我开始进行故障排除并发现了一个奇怪的错误,有人可以向我解释一下。

If I pick up the fields and doing this it becomes result1 == false and result2 == true, why? 如果我选择字段并执行此操作,结果将为result1 == false和result2 == true,为什么?

        MyClass m1 = new MyClass();
        MyClass m2 = new MyClass();

        Field[] fieldsFirst = m1.getClass().getDeclaredFields();
        Field[] fieldsSecond =  m2.getClass().getDeclaredFields();

        for (int i = 0; i < fieldsFirst.length; i++) {

            Field first = fieldsFirst[i];
            Field second = fieldsSecond[i];

            first.setAccessible(true);
            second.setAccessible(true);

            if(first.get(m1) instanceof Boolean)
            {
               boolean b1 = (Boolean)first.get(m1); 
               boolean b2 = (Boolean)second.get(m2);

               //Here are the results
               boolean result1 = b1 != b2; // false
               boolean result2 = (Boolean)first.get(m1) != (Boolean)second.get(m2); // true

            }

If I have: 如果我有:

public class MyClass {

private boolean myBoolean = true;

public boolean getMyBoolean()
{
return myBoolean;
}
public void setMyBoolean(booelan inBool)
{
myBoolean = inBool;
}

}

In

boolean result1 = b1 != b2; // false

you are comparing primitive values, as b1 and b2 result from unboxing conversion from Boolean to boolean . 您正在比较原始值,因为b1b2是从Booleanboolean 拆箱转换结果。

In

boolean result2 = (Boolean)first.get(m1) != (Boolean)second.get(m2); // true

you are comparing references. 您正在比较参考。 The result of each get() is referencing a different object. 每个get()的结果引用了一个不同的对象。 As such, the != comparison is true . 因此, !=比较为true

Boolean is a wrapper around the primitive boolean . Boolean是原始boolean的包装。

a wrapper class wraps (encloses) around a data type and gives it an object appearance. 包装器类将数据类型包装(封装)并赋予其对象外观。 Wherever, the data type is required as an object, this object can be used. 在任何需要数据类型作为对象的地方,都可以使用此对象。 Wrapper classes include methods to unwrap the object and give back the data type. 包装器类包括解包装对象并返回数据类型的方法。

Source: http://way2java.com/java-lang/wrapper-classes/ 来源: http : //way2java.com/java-lang/wrapper-classes/

Just like with other objects, if you need to compare their values, you need to use .equals() and not the comparison operators. 与其他对象一样,如果需要比较它们的值,则需要使用.equals()而不是比较运算符。

Here: 这里:

boolean b1 = (Boolean)first.get(m1); 
boolean b2 = (Boolean)second.get(m2);  

You are converting the Boolean to boolean . 您正在将Boolean转换为boolean This is called as unboxing conversion which is a part of the AutoBoxing Conversions . 这称为unboxing conversion ,这是“ AutoBoxing Conversions的一部分。 These are called Auto because Java automatically does that conversion for you; 这些之所以称为“ Auto是因为Java会自动为您进行转换。 even if you get rid of the cast. 即使您摆脱了演员表。
Hence, you are comparing their primitive values. 因此,您正在比较它们的原始值。
Since the primitive values are the same, your comparison evaluates to true. 由于原始值相同,因此您的比较结果为true。 Hence, false 因此, false

Here: 这里:

boolean result2 = (Boolean)first.get(m1) != (Boolean)second.get(m2);  

You are comparing the references of the two objects. 您正在比较两个对象的引用。 Since they are stored in different memory locations, the result of the comparison is true . 由于它们存储在不同的存储位置,因此比较结果为true Intuitive, isn't it? 直观,不是吗? Different object, different memory location? 不同的对象,不同的内存位置? .

If you really need to compare the values of two objects, use the equals() methods. 如果您确实需要比较两个对象的值,请使用equals()方法。 In case of Wrapper objects however, close your eyes, unbox them to their primitive values and then compare. 但是,对于Wrapper对象,请闭上眼睛,将其拆箱为其原始值,然后进行比较。

Just like Josh Bloch suggests in Effective Java: Comparing Wrappers? 就像乔什·布洛赫(Josh Bloch)在《有效的Java: 比较包装程序》中所建议的那样 Unbox the suckers!! 开箱吸盘!
Just more from Effective Java, comparison operators work in case of Wrapper class if they have a greater than or less than sign attached to them. 比较有效的Java而言,如果比较运算符具有大于或小于的附加符号,则适用于Wrapper类。 < , <= , > , >= yield the correct result even if you do not unbox. <<=>>=产生正确的结果,即使您未取消装箱。 == and != do not yield correct result ==!=不会产生正确的结果

The comparation boolean result1 = b1 != b2 is a primitive value comparation, so you can use operators like == or !=. 比较boolean result1 = b1 != b2是原始值比较,因此您可以使用==或!=之类的运算符。

The comparation boolean result2 = (Boolean)first.get(m1) != (Boolean)second.get(m2) is an object comparation, so you are not comparing values, but references. 比较boolean result2 = (Boolean)first.get(m1) != (Boolean)second.get(m2)是对象比较,因此您不是在比较值,而是在引用。 You should compare them by using equals() 您应该使用equals()比较它们

Like boolean result2 = ((Boolean)first.get(m1)).equals((Boolean)second.get(m2)) boolean result2 = ((Boolean)first.get(m1)).equals((Boolean)second.get(m2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM