How do I compare strings in Java?
==
will work some of the time, as java has a String pool, where it tries to reuse memory references of commonly used strings. But ==
compares that objects are equal, not the values... so .equals()
is the proper use you want to use.
Also its good to know that, if you are overridding .equals () method, make sure you are overridding .hashcode () method, otherwise you will end up with violating equivalence relation b/w equals and hashcode.