Java String compare to determine Equality
java string compare can be done in many ways as shown below. Depending on the type of java stringcompare you need, each of them is used.* == Operator
* equals method
* compareTo method
Comparing using the == Operator
The == operator is used when we have to compare the String object references. If two String variables point to the same object in memory, the comparison returns true. Otherwise, the comparison returns false. Note that the ‘==’ operator does not compare the content of the text present in the String objects. It only compares thereferences the 2 Strings are pointing to. The following Program would print “The strings are unequal” In the first case and “The strings are equal” in the second case.
public class StringComparision1 {
public static void main(String[] args) {
String name1 = "Bob";
String name2 = new String("Bob");
String name3 = "Bob";
// 1st case
if (name1 == name2) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are unequal.");
}
// 2nd case
if (name1 == name3) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are unequal.");
}
}
} |
Comparing using the equals Method
The equals method is used when we need to compare the content of the text present in the String objects. This method returns true when two String objects hold the same content (i.e. the same values). The following Program would print “The strings are unequal” In the first case and “The strings are equal” in the second case.
public class StringComparision2 { public static void main(String[] args) { String name1 = "Bob"; String name2 = new String("Bob1"); String name3 = "Bob"; // 1st case if (name1.equals(name2)) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } // 2nd case if (name1.equals(name3)) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } } } |
Comparing using the compareTo Method
The compareTo method is used when we need to determine the order of Strings lexicographically. It compares char values similar to the equals method. The compareTo method returns a negative integer if the first Stringobject precedes the second string. It returns zero if the 2 strings being compared are equal. It returns a positive integer if the first String object follows the second string. The following Program would print “name2 follows name1” In the first case and “name1 follows name3” in the second case.
public class StringComparision3 { public static void main(String[] args) { String name1 = "bob"; String name2 = new String("cob"); String name3 = "Bob"; // 1st case if (name1.compareTo(name2) == 0) { System.out.println("The strings are equal."); } else if (name1.compareTo(name2) < 0) { System.out.println("name2 follows name1"); } else { System.out.println("name1 follows name2"); } // 2nd case. Comparing Ascii Uppercase will be smaller then Lower Case if (name1.compareTo(name3) == 0) { System.out.println("The strings are equal."); } else if (name1.compareTo(name3) < 0) { System.out.println("name3 follows name1"); } else { System.out.println("name1 follows name3"); } } } |
No comments:
Post a Comment