Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does == check for full equality in Booleans? - Java

So I've heard that if I compare 2 strings with == then I will only get true back if they both refer to the same object/instance. That's strings. What about Booleans?

like image 504
Bluefire Avatar asked Jun 17 '12 16:06

Bluefire


People also ask

Can we use == to compare boolean?

This method returns an integer value. It returns value 0, if x==y. It returns positive value, if x is true and y is false. It returns a negative value, if x is false and y is true.

How do you know if booleans are equal?

boolean isEqual = Boolean. equals(bool1, bool2); which should return false if they are not equal, or true if they are.

Can you compare two Booleans Java?

We use the compare() method of the BooleanUtils class to compare two boolean values. The method takes two values and returns true if both the values are the same. Otherwise, it returns false .

How do you do if statements with booleans in Java?

The simplest if-statement has two parts -- a boolean "test" within parentheses ( ) followed by "body" block of statements within curly braces { }. The test can be any expression that evaluates to a boolean value -- true or false. The if-statement evaluates the test and then runs the body code only if the test is true.


2 Answers

Does == check for full equality in Booleans? - Java

It depends on whether you're talking about Booleans (the object wrapper, note the capital B) or booleans (the primitive, note the lower case b). If you're talking about Booleans (the object wrapper), as with all objects, == checks for identity, not equivalence. If you're talking about booleans (primitives), it checks for equivalence.

So:

Boolean a, b; a = new Boolean(false); b = new Boolean(false); System.out.println("a == b? " + (a == b)); // "a == b? false", because they're not the same instance 

But

boolean c, d; c = false; d = false; System.out.println("c == d? " + (c == d)); // "c == d? true", because they're primitives with the same value 

Regarding strings:

I've heard that if I compare 2 strings with == then I will only get true back if the strings are identical and they both refer to the same object/instance...

It's not really an "and": == will only check whether the two String variables refer to the same String instance. Of course, one String instance can only have one set of contents, so if both variables point to the same instance, naturally the contents are the same... :-) The key point is that == will report false for different String instances even if they have the same characters in the same order. That's why we use equals on them, not ==. Strings can get a bit confusing because of interning, which is specific to strings (there's no equivalent for Boolean, although when you use Boolean.valueOf(boolean), you'll get a cached object). Also note that Java doesn't have primitive strings like it does primitive boolean, int, etc.

like image 128
T.J. Crowder Avatar answered Oct 08 '22 05:10

T.J. Crowder


If you have an Object use equals, when not you can run in things like this. (VM cache for autoboxing primitives)

    public static void main(String[] args){        Boolean a = true;        Boolean b = true;        System.out.println(a == b);        a = new Boolean(true);        b = new Boolean(true);        System.out.println(a == b);    } 

the output is TRUE and FALSE

like image 23
kaytastrophe Avatar answered Oct 08 '22 04:10

kaytastrophe