What's the difference in STRING.equals("myValue") vs STRING == "myValue"?
I first used STRING == "myValue" but my IDE recommends to switch to using .equals(). Is there a specific benefit to doing this?
What's the difference in STRING.equals("myValue") vs STRING == "myValue"?
I first used STRING == "myValue" but my IDE recommends to switch to using .equals(). Is there a specific benefit to doing this?
Yes. Using == only compares the reference values; the equals() function actually checks whether or not the string contents are identical.
String x = new String("foo");
String y = new String("foo");
System.out.println(x == y); // prints false
System.out.println(x.equals(y)); // prints true
I'm pretty sure == compares references, not values, while the .equals() does the value comparison.
Yes, STRING == "myValue" won't work, because it will attempt to compare the references to the strings, and not the strings themselves.
.equals() way is the correct way and will give you the response you want.