0

In java why do you have to use String.equals() for equivalence. Why cant you use == like the other data types?

22nd Feb 2017, 6:15 AM
derek
2 odpowiedzi
+ 5
Alvaro is right. Sometimes you need to compare whether two objects are the same objects (==), sometimes you have to check if they are equals although they are not the same (equals).
22nd Feb 2017, 8:46 AM
Tamás Barta
Tamás Barta - avatar
+ 4
AFAIK strings are stored roughly as objects. When you compare two objects via ==, you're not comparing the actual contents of the objects, but the respective pointers instead. That is, a==b evaluates whether a and b point to *the same* object (aka String). In order to compare the actual contents, you need .equals().
22nd Feb 2017, 6:20 AM
Álvaro