Why does == work on integers but not on strings?<>
This question already has an answer here:
- How do I compare strings in Java? 23 answers
Because the value of an object is the memory location which holds the object values, and the value of primitive types is its value itself. If you're using == on objects of type String for example, you are comparing their memory locations.
== compares references because string is an Reference type while int is a value type.
That's because in Java strings are objects, and objects will only be equal if their reference is. (They are the same object)
For string, you have to use equals() instead of == because it is an object.