32

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?

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
Bluefire
  • 13,519
  • 24
  • 74
  • 118

4 Answers4

55

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • So if I use `new boolean` to make my variable, as opposed to `new Boolean`, `==` will be the same as `equals()`? – Bluefire Jun 17 '12 at 16:25
  • 1
    @Bluefire: You don't use `new boolean` (lower case) at all. You use `true` or `false` or the result of a comparison operation. I think there are very few use cases in modern Java for `Boolean`. – T.J. Crowder Jun 17 '12 at 16:25
  • So to declare a primitive boolean, do I put something like `boolean myBoolean = true`? – Bluefire Jun 17 '12 at 16:27
  • @Bluefire: Yup. Or `boolean myBoolean = false;` or just `boolean myBoolean;` if you don't want to initialize it under later. Primitives don't need to be constructed (so just like `int n = 5;` or just `int n;`, `boolean b = false;` or just `boolean b;`). – T.J. Crowder Jun 17 '12 at 16:28
  • 4
    @Bluefire Note that evne if you really want a `Boolean` you should never call `new Boolean`, but instead use `Boolean.TRUE` respectively `Boolean.FALSE`. If you follow that rule you can actually compare them with `==` (although you probably still shouldn't). – Voo Jun 17 '12 at 17:47
  • so, by not having to re-verify all the code for if it is a primitive or wrapper, the best option is simply use `Boolean.compare(x,y)==0` everywhere right? – Aquarius Power Dec 02 '15 at 03:10
  • @AquariusPower: I've never had code where I was unsure whether I was dealing with a primitive or a wrapper. – T.J. Crowder Dec 02 '15 at 06:08
  • @T.J.Crowder I actually meant full code refactoring thru `sed` regex (I actually use [secRefactorInFiles.sh](http://sourceforge.net/p/scriptechocolor/git/ci/master/tree/ScriptEchoColor/bin.extras/secRefactorInFiles.sh)), so I will not be checking each, and will just test if the application still works, but I believe the application will work better by avoiding the b1==b2 confusion. Obs.: my code has standards like booleans begin with "b", so regex will work properly. – Aquarius Power Dec 02 '15 at 16:05
4

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

  • this is not improving the answer, please add an answer only if it improves already submitted answers – Soumya R Jun 22 '15 at 18:47
  • Actually original answer lacks autoboxing example. I came across the following code: Optional a = f(); Optional b = g(); if (a.isPresent() && b.isPresent() && a.get() != b.get()) {...} which looks like a bug. The reason it is not a bug is that f() and g() were returning things like Optional.of(true), etc. and auto-boxing feature made it correct. Note, however, that for ints auto-boxing "caches" only values up-to 127 or something like this, so it is better to avoid relying on "caching". – Yevgen Yampolskiy Jul 30 '16 at 04:12
  • When you use Boolean instead of boolean, you shoud init it with Boolean.TRUE or Boolean.FALSE. – horvoje Mar 07 '22 at 11:49
4

When using ( == ) with booleans,

  1. If one of the operands is a Boolean wrapper, then it is first unboxed into a boolean primitive and the two are compared.

  2. If both are Boolean wrappers,created with 'new' keyword, then their references are compared just like in the case of other objects.
    new Boolean("true") == new Boolean("true") is false

  3. If both are Boolean wrappers,created without 'new' keyword,

    Boolean a = false;
    Boolean b = Boolean.FALSE;
    // (a==b) return true
    
dkb
  • 4,389
  • 4
  • 36
  • 54
Yuresh Karunanayake
  • 519
  • 1
  • 4
  • 10
1

It depends if you are talking about value types like: int, boolean, long or about reference types: Integer, Boolean, Long. value types could be compared with ==, reference types must be compared with equals.

0lukasz0
  • 3,155
  • 1
  • 24
  • 40
  • Not always true. If you use Boolean and init it with Boolean.FALSE or Boolean.TRUE, you can make comparison with == – horvoje Mar 07 '22 at 11:50