Be careful with the use of == with Strings.
Sometimes it looks as if = = compares String values. Using the = = operator, compares the memory addresses and not the actual string text. While use of = = may compile, it does not work reliably. Avoid!!!
The most widely used method to compare String variables is the equals method. The equals method tests two String variables (or any two objects) to see if they have equal values. This equals method examines two strings, and if they contain the exact same string of characters, the strings are considered equal. This method is case sensitive.
s1.equals(s2) //checks for equality
s1.equalsIgnoreCase(s2) //checks for equality ignoring case
String name = "tom";
String name2 = "joe";
if (name.equals(name2))
System.out.println("Yes");
else
System.out.prinln("No");
No will be printed on the screen. |