Java String compare to determine Equality(Java String类型比较的三种方法)

javastringcomparecan be done in many ways as shown below. Depending on the type ofjavastringcompareyou need, each of them is used.

Comparing using the ==Operator

The == operator is used when we have tocomparethe 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 notcomparethe content of the textpresentin the String objects. It only compares the references the 2 Strings are pointing to. The following Program wouldprint“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 equalsMethod

The equals method is used when we need tocomparethe content of the textpresentin the String objects.This methodreturns true when two String objectsholdthe same content (i.e. the same values). The following Program wouldprint“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 compareToMethod

The compareTo method is used when we need to determine theorderof Strings lexicographically. It compares char values similar to the equalsmethod. The compareTomethodreturns anegativeinteger if the first String object 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 wouldprint“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");
		}
	}
}

Reference:
http://www.javabeginner.com/learn-java/java-string-comparison


END

你可能感兴趣的:(java String)