Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Boolean</code> is a wrapper around the primitive <code>boolean</code>. </p> <blockquote> <p>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. </p> </blockquote> <p>Source: <a href="http://way2java.com/java-lang/wrapper-classes/" rel="nofollow">http://way2java.com/java-lang/wrapper-classes/</a> </p> <p>Just like with other objects, if you need to compare their values, you need to use <code>.equals()</code> and not the comparison operators. </p> <p>Here: </p> <pre><code>boolean b1 = (Boolean)first.get(m1); boolean b2 = (Boolean)second.get(m2); </code></pre> <p>You are converting the <code>Boolean</code> to <code>boolean</code>. This is called as <code>unboxing conversion</code> which is a part of the <code>AutoBoxing Conversions</code>. These are called <code>Auto</code> because Java automatically does that conversion for you; even if you get rid of the cast.<br> Hence, you are comparing their primitive values.<br> Since the primitive values are the same, your comparison evaluates to true. Hence, <code>false</code> </p> <p>Here: </p> <pre><code>boolean result2 = (Boolean)first.get(m1) != (Boolean)second.get(m2); </code></pre> <p>You are comparing the references of the two objects. Since they are stored in different memory locations, the result of the comparison is <code>true</code>. <em>Intuitive, isn't it? Different object, different memory location?</em>. </p> <p>If you really need to compare the values of two objects, use the <code>equals()</code> methods. In case of <code>Wrapper</code> objects however, close your eyes, unbox them to their primitive values and then compare.</p> <p><strong>Just like Josh Bloch suggests in Effective Java: <em>Comparing Wrappers? Unbox the suckers!!</em></strong><br> <em>Just more from Effective Java, comparison operators work in case of <code>Wrapper</code> class if they have a greater than or less than sign attached to them. <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code> yield the correct result even if you do not unbox. <code>==</code> and <code>!=</code> do not yield correct result</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload