equals():Compare two string value for equality

boolean equals(Object anObject)
Compares this string to the specified object.
boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations.

public class MainClass {
  public static void main(String args[]) {
  String s1 = "Hello";
  String s2 = "Hello";
  String s3 = "Goodbye";
  String s4 = "HELLO";
  System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
  System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
  System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
  }
}

/*
 * Output:


  Hello equals Hello -> true
  Hello equals Goodbye -> false
  Hello equals HELLO -> false
   
 * */

The following code checks the length of a string, then gets the fourth char out of the string object and compares these two string objects.


public class Main {
  public static void main(String args[]) {
    String strOb1 = "java2s.com";
    String strOb2 = "java2s.com";
    String strOb3 = strOb1;

    System.out.println("Length of strOb1: " + strOb1.length());
    System.out.println("Char at index 3 in strOb1: " + strOb1.charAt(3));

    if (strOb1.equals(strOb2))
      System.out.println("strOb1 == strOb2");
    else
      System.out.println("strOb1 != strOb2");

    if (strOb1.equals(strOb3))
      System.out.println("strOb1 == strOb3");
    else
      System.out.println("strOb1 != strOb3");
  }
}

This program generates the following output:


Length of strOb1: 10
Char at index 3 in strOb1: a
strOb1 == strOb2
strOb1 == strOb3

The comparison is case-sensitive.


public class Main {
  public static void main(String args[]) {
    String s1 = "Hello";
    String s2 = "Hello";
    String s3 = "Good-bye";
    String s4 = "HELLO";
    System.out.println(s1 + " equals " + s2 + " -> " +

    s1.equals(s2));
    System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
    System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
    System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + s1.equalsIgnoreCase(s4));
  }
}
  

The output:



Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true
  

To compare igoring case, call equalsIgnoreCase( ). It has this general form:


boolean equalsIgnoreCase(String str)

str is the String object being compared with the invoking String object. Here is an example that demonstrates equals( ) and equalsIgnoreCase( ):


public class Main {
  public static void main(String args[]) {
    String s1 = "Hello";
    String s2 = "Hello";
    String s3 = "Good-bye";
    String s4 = "HELLO";
    System.out.println(s1 + " equals " + s2 + " -> " +

    s1.equals(s2));
    System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
    System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
    System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + s1.equalsIgnoreCase(s4));
  }
}
  

The output from the program is shown here:


Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true
  
Home 
  Java Book 
    Essential Classes  

String:
  1. String type and Literals
  2. String Concatenation
  3. String.CASE_INSENSITIVE_ORDER
  4. String Constructor
  5. charAt(int index):Get a single char by index
  6. String: compareTo(String stringValue)
  7. concat(String str)
  8. equals():Compare two string value for equality
  9. equals( ) vs ==
  10. contains(CharSequence s)
  11. copyValueOf(char[] data)
  12. endsWith(String suffix)
  13. format():Format a string
  14. getBytes():Get byte array from string
  15. getChars()
  16. indexOf
  17. intern a string
  18. isEmpty:if string is empty
  19. lastIndexOf()
  20. length() Returns the length of this string
  21. startsWith( )
  22. toLowerCase() and toUpperCase(): convert string case with locale
  23. substring:Get sub string from a string
  24. toCharArray():Get char array from string
  25. toString( )
  26. trim()
  27. valueOf():Convert boolean, char, double, float,int,long,object to String