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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on whether you’re talking about
Booleans (the object wrapper, note the capitalB) orbooleans (the primitive, note the lower caseb). If you’re talking aboutBooleans (the object wrapper), as with all objects,==checks for identity, not equivalence. If you’re talking aboutbooleans (primitives), it checks for equivalence.So:
But
Regarding strings:
It’s not really an “and”:
==will only check whether the twoStringvariables refer to the sameStringinstance. Of course, oneStringinstance 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 reportfalsefor differentStringinstances even if they have the same characters in the same order. That’s why we useequalson them, not==. Strings can get a bit confusing because ofinterning, which is specific to strings (there’s no equivalent forBoolean, although when you useBoolean.valueOf(boolean), you’ll get a cached object). Also note that Java doesn’t have primitive strings like it does primitiveboolean,int, etc.