Is there ever a situation where using equals(Boolean)
and ==
would return different results when dealing with Boolean
objects?
Boolean.TRUE == myBoolean;
Boolean.TRUE.equals(myBoolean);
I'm not thinking about primitive types here, just Boolean objects.
Boolean values are values that evaluate to either true or false , and are represented by the boolean data type. Boolean expressions are very similar to mathematical expressions, but instead of using mathematical operators such as "+" or "-", you use comparative or boolean operators such as "==" or "!".
Boolean. TRUE is a reference to an object of the class Boolean, while true is just a value of the primitive boolean type. Classes like Boolean are often called "wrapper classes", and are used when you need an object instead of a primitive type (for example, if you're storing it in a data structure).
Typically, you use == and != with primitives such as int and boolean, not with objects like String and Color. With objects, it is most common to use the equals() method to test if two objects represent the same value.
The equals() method of Boolean class is a built in method of Java which is used check equality of two Boolean object. Parameter: It take a parameter ob of type Object as input which is the instance to be compared. Return Type: The return type is boolean.
How about:
System.out.println(new Boolean(true) == new Boolean(true));
System.out.println(new Boolean(true) == Boolean.TRUE);
(both print false, for the same reason as any other type of objects).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With