Always confused about this stuff. Anyone can help?
Java String equals() Method The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.
Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.
The right way of comparing String in Java is to either use equals(), equalsIgnoreCase(), or compareTo() method. You should use equals() method to check if two String contains exactly same characters in same order. It returns true if two String are equal or false if unequal.
The equals() tells the equality of two strings whereas the compareTo() method tell how strings are compared lexicographically.
string1.equals(string2)
is the way.
It returns true
if string1
is equals to string2
in value. Else, it will return false
.
equals reference
string1.equals(string2)
is right way to do it.
String s = "something", t = "maybe something else"; if (s == t) // Legal, but usually results WRONG. if (s.equals(t)) // RIGHT way to check the two strings /* == will fail in following case:*/ String s1 = new String("abc"); String s2 = new String("abc"); if(s1==s2) //it will return false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With