Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean.TRUE == myBoolean vs. Boolean.TRUE.equals(myBoolean)

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.

like image 936
Edd Avatar asked May 08 '13 09:05

Edd


People also ask

Do you use or == for Boolean?

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 "!".

What is the difference between Boolean true and true?

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).

Can you use == for Boolean Java?

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.

What is Boolean equal?

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.


1 Answers

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).

like image 139
assylias Avatar answered Sep 17 '22 20:09

assylias