1

I don't understand why the operator == won't work when comparing string, but it will work when comparing int, double, float, etc.

From what I understand, == determines if two entities are pointing to the same object.
In the following code, a == b returns true. Why?

int a = 10;
int b = 10;

if (a == b) {
  // …
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
john
  • 29
  • 1
  • This is NOT a duplicate of the suggested question. The suggested duplicate is about how Strings should be compared and what's the difference between == and equals there. This question is about the different behavior of == between Strings and elementary data types. This deserves an answer. – mastov Nov 20 '14 at 13:00
  • Please take more than 31 seconds (!) to take a decision like this. This is not a race and quick action is not the same thing as good action, especially if your action prevents others from answering the question. Now is there a way to undo the damage? – mastov Nov 20 '14 at 13:42
  • @mastov The first step is to assume the best, when explaining why you think someone else made a mistake. Just be a little more gentle about it ;) That said; I think you are right here, and it took me *less than 31 seconds* to reach that conclusion. Finally; this might actually be a dupe of something else... – Andrew Barber Nov 22 '14 at 22:00
  • @AndrewBarber: You are right. It just bothered me that there was talk about some kind of record for the quickest marking as duplicate, when at the same time the marking had been done wrong. And yes, this is probably a duplicate of something else, but the point is in referencing the right duplicate (so the poster actually receives a reference to an answer). – mastov Nov 23 '14 at 14:19
  • @AndrewBarber: Btw. the original comments about the "record" that I'm talking about seem to have been deleted. – mastov Nov 23 '14 at 14:20
  • @mastov yup, I deleted them. Only left yours so you would get the notice of my reply. – Andrew Barber Nov 23 '14 at 16:48

2 Answers2

2

I don't understand why the operator "==" will not work for comparing string, but will for comparing int, double, float, etc. from what i understand, "==" determines if two entities are pointing to the same object.

The root of your misunderstanding is that the primitive values like 1 and false and 3.14159 are NOT objects. They are values that DO NOT have an object identity.

In Java there are two kinds of type:

  • Primitive data types ... for which == compares the values
  • Reference types ... for which == compares the object identity

a and b are have different reference in memory but this statement returns true.

Now you are confusing something else. a and b are actually variables. And yes, behind the scenes variables do have corresponding memory addresses.

However ... a == b DOES NOT compare the addresses the variables. It compares the content of the variables. And the content of the variables are primitive values that don't have an address.

i thought "==" compares reference?

For reference types (objects and arrays), the == operator compares references.

For primitive types, the == operator compares the actual values.

Think about it. You want 42 to be equal to 42 ... no matter how the numbers were generated.

And just to get back to String. Despite being "built in" to the Java language, the String type is a reference type (the class java.lang.String) and == compares it using reference type semantics; i.e. by comparing identity. This is a cause of considerable confusion for new Java programmers (hence the knee-jerk closure), but it is a perfectly logical if you understand the bigger picture.


Now, things do get more complex when we throw the wrapper types and boxing / unboxing into the mix. But that is beyond the scope of your question.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You are right when comparing strings, but that is because strings are Reference types. == test for the same reference, not on the value, on Reference types.

But types like int, boolean, long, char are primitive, or "value" types, not reference types. That is; the variable is indicating the actual value; not the reference to where the value is stored.

Note that there are versions of the above that are reference types; the versions with capitals: Int, Boolean, Long, Char. These represent 'boxed' versions of the primitives above, and == compares references with them.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123