31

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?

4
  • 4
    How could the strings refer to the same instance and not be identical? Commented Jun 17, 2012 at 16:22
  • 1
    No, what I mean is if they are identical but don't refer to the same instance, == returns false. Commented Jun 17, 2012 at 16:23
  • 1
    Please read anything before posting, and don't start with I heard something somewhere. Commented Jun 17, 2012 at 16:30
  • I've heard that = I know that in the above case. Not saying you should have known that, but just clarifying. Commented Jun 17, 2012 at 16:35

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

8 Comments

So if I use new boolean to make my variable, as opposed to new Boolean, == will be the same as equals()?
@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.
So to declare a primitive boolean, do I put something like boolean myBoolean = true?
@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;).
@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).
|
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

3 Comments

this is not improving the answer, please add an answer only if it improves already submitted answers
Actually original answer lacks autoboxing example. I came across the following code: Optional<Boolean> a = f(); Optional<Boolean> 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".
When you use Boolean instead of boolean, you shoud init it with Boolean.TRUE or Boolean.FALSE.
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 

Comments

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.

1 Comment

Not always true. If you use Boolean and init it with Boolean.FALSE or Boolean.TRUE, you can make comparison with ==

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.