Can you compare String using == ? Yes. Works 100% of the time? No.
One of the first things I learned when I started programming in java was NEVER use == to compare Strings, but Why? Let's go to a technical explanation.
String is an object, the method equals (Object) will return true if both strings have the same object. The == operator will only return true if both references String references point to the same object.
When we create a String is literally created a pool of strings, when we create another String literal with the same value if the JVM demand already exists a String with the same value in the String pool, if any, does your variable to point to the same memory address.
For this reason, when you test the equality of the variables "a" and "b" using "==", could be returns true.
Example:
String a = "abc" / / string pool
String b = "abc"; / * already exists a string with the same content in the pool,
go to the same reference in memory * /
String c = "dda" / / go to a new reference of memory as there is in pool
If you create strings so you are creating a new object in memory and test the equality of the variables a and b with "==", it returns false, it does not point to the same place in memory.
String d = new String ("abc") / / Create a new object in memory
String a = "abc";
String b = "abc";
String c = "dda";
String d = new String ("abc");
a == b = true
a == c = false
a == d = false
a.equals (d) = true