Java String equals() Method

Java String equals() Method | The equals() method of the String class is used to compare the two strings. It returns true if both strings are content-wise equal and return false if given strings are not equal.

The equals() method is mainly defined in the Java Object class, and it compares objects based on their reference. If we want to compare objects based on their content/data then we have to override the equals() method in our class. Java String class also follows the concept and overrides equals() method to compare strings content or data-wise.

Whereas StringBuilder and StringBuffer override the equals() method hence their objects are compared based on the reference. We have discussed it in detail in the Object class equals() method.

Method signature:- public boolean equals(Object object)
Parameter: a string that needs to be compared.
Return type: boolean (true or false).

See the below example for String equality in Java,
Example-1:-
String1 = “Hey”
String2 = “Hey”
Result => True

Example-2:-
String1 = “Hey”
String2 = “Hi”
Result => False

Java String equals() Method Example

public class Main {
   public static void main(String args[]) {
      String str1 = "Java Programming";
      String str2 = "Java Programming";
      String str3 = "JAVA PROGRAMMING";
      String str4 = "Java";

      System.out.println(str1.equals(str2));
      System.out.println(str1.equals(str3));
      System.out.println(str1.equals(str4));
   }
}

Output:-

true
false
false

Not only two strings, but we can also compare strings with other objects but in that case, we will always get the result “false” because they are not two different objects and are not comparable with strings. Let us understand it through an example:-

public class Main {
   public static void main(String args[]) {
      String string = "KnowProgram";
      StringBuilder sb = new StringBuilder("KnowProgram");
      StringBuffer sbf = new StringBuffer("KnowProgram");
      Main main = new Main();

      System.out.println(string.equals(sb));
      System.out.println(string.equals(sbf));
      System.out.println(string.equals(main));
   }
}

Output:-

false
false
false

How To Check If Two Strings Are Equal In Java

There are multiple ways using which we can check if two strings are equal in Java or not. Java String class contains equals(), equalsIgnoreCase(), compareTo(), and compareToIgnoreCase() method. But using the equals() method to check string equality in Java will be the easy and appropriate way.

public class Main {
   public static void main(String[] args) {

      String str1 = "Know Program";
      String str2 = "Know Program";
      String str3 = "knowprogram";

      System.out.println(str1.equals(str2));

      if (str1.equals(str3)) {
         System.out.println("Both strings are equal");
      } else {
         System.out.println("Both strings are not equal");
      }
   }
}

Output:-

true
Both strings are not equal

How To Compare Two Strings In Java Without Using equals()

If we want to compare two strings without using the equals() method then we can use the compareTo() method. The equals() method returns a boolean value whereas compareTo() returns 0 if both strings are equals, or else returns the difference between the first unmatched characters.

public class Main {
   public static void main(String[] args) {
      String str1 = "Know Program";
      String str2 = "Know Program";
      String str3 = "knowprogram";

      if (str1.compareTo(str2) == 0) {
         System.out.println("Strings str1 & str2 are equal");
      } else {
         System.out.println("Strings str1 & str2 are not equal");
      }

      if (str1.compareTo(str3) == 0) {
         System.out.println("Strings str1 & str3 are equal");
      } else {
         System.out.println("Strings str1 & str3 are not equal");
      }
   }
}

Output:-

Strings str1 & str2 are equal
Strings str1 & str3 are not equal

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *