Java Data Type Tutorial - Java String Compare








The String class overrides the equals() method of the Object class and provides its own implementation, which compares two strings for equality based on their contents.

Equals

For example, we can compare two strings for equality, as shown:

String str1 = new String("Hello"); 
String str2 = new String("Hi"); 
String str3 = new String("Hello");

boolean b1,   b2;

b1  = str1.equals(str2); // false will be  assigned to b1 
b2  = str1.equals(str3); // true will be  assigned to b2

We can also compare string literals with string literals or string objects, as shown:

b1  = str1.equals("Hello");   // true will be  assigned to b1 
b2  = "Hello".equals(str1);   // true will be  assigned to b2 
b1  = "Hello".equals("Hi");   // false will be  assigned to b1

== operator always compares the references of two objects in memory.

str1 == str2 and str1 == str3 will return false, because str1, str2 and str3 are references of three different String objects in memory.





Compare

To compare two strings based on the Unicode values of their characters, use the compareTo() method. Its signature is

public int compareTo(String anotherString)

It returns an integer, which can be 0 (zero), a positive integer, or a negative integer.

The method returns the difference between the Unicode values of those two characters.

For example, "a".compareTo("b") will return -1. The Unicode value is 97 for a and 98 for b. It returns the difference 97 - 98, which is -1.

The following are examples of string comparisons:

"abc".compareTo("abc") will  return  0
"abc".compareTo("xyz") will  return  -23  (value of  'a' -  'x') 
"xyz".compareTo("abc") will  return  23  (value of  'x' -  'a')

The following code shows how to do the string comparisons.

public class Main {
  public static void main(String[] args) {
    String apple = new String("Apple");
    String orange = new String("Orange");
    System.out.println(apple.equals(orange));
    System.out.println(apple.equals(apple));
    System.out.println(apple == apple);/* w  w w. j av a  2 s  .c o m*/
    System.out.println(apple == orange);
    System.out.println(apple.compareTo(apple));
    System.out.println(apple.compareTo(orange));
  }
}

The code above generates the following result.





String Pool

Java maintains a pool of all string literals. It creates a String object in the string pool for every string literal.

When it encounters a string literal, it looks for a string object in the string pool with the identical content. If it does not find a match in the string pool, it creates a new String object and adds it to the string pool.

If it finds a match in the string pool, it replaces the string literal with the reference of the String object found in the pool.

We can add a String object to the string pool using its intern() method.

The intern() method returns the reference of the object from string pool if it finds a match. Otherwise, it adds a new String object to the string pool and returns the reference of the new object.

String Case Compare

To compare two strings for equality ignoring their cases, use the equalsIgnoreCase() method.

To perform a case-sensitive comparison for equality, use the equals() method.

public class Main {
  public static void main(String[] args) {
    String str1 = "Hello";
    String str2 = "HELLO";
/*www  .  j  a v a  2  s .co m*/
    if (str1.equalsIgnoreCase(str2)) {
      System.out.println("Ignoring case str1 and str2 are equal");
    } else {
      System.out.println("Ignoring case str1 and str2 are not equal");

    }
    if (str1.equals(str2)) {
      System.out.println("str1 and str2 are equal");
    } else {
      System.out.println("str1 and str2 are not equal");
    }
  }
}

The code above generates the following result.

Language-Sensitive String Comparison

The String class compares strings based on the Unicode values of their characters.

To compare strings based on the dictionary order, use the compare() method of the java.text.Collator class to perform language-sensitive (dictionary order) string comparisons.

The method takes two strings to be compared as arguments. It returns 0 if two strings are the same, 1 if the first string comes after the second, and -1 if the first string comes before the second.

The following code illustrates the use of the Collator class.

import java.text.Collator;
import java.util.Locale;
//ww  w . j  a  v  a2  s. c o m
public class Main {
  public static void main(String[] args) {
    Locale USLocale = new Locale("en", "US");

    Collator c = Collator.getInstance(USLocale);
    String str1 = "Java";
    String str2 = "HTML";
    int diff = c.compare(str1, str2);
    System.out.print("Comparing using Collator  class: ");

    if (diff > 0) {
      System.out.println(str1 + "  comes after " + str2);
    } else if (diff < 0) {
      System.out.println(str1 + "  comes before " + str2);
    } else {
      System.out.println(str1 + "  and  " + str2 + "  are   the   same.");
    }
  }
}

The code above generates the following result.