Site icon TechVidvan

Java String equals() Method

A primary way for determining whether the contents of two objects are equal in Java is the equals() function. It is used particularly to compare the contents of two String objects in the context of the String.

Explanation

The String equals() function in Java compares two strings based on their data/content.

It returns true if all of the contents of both strings match. It returns false if none of the characters match.

How do use Strings equals() in Java:

Syntax:

public boolean equals(Object anotherObject)

Parameters:

anotherObject is described in the parameterAn object that represents the competing string for comparison

Return value:

A boolean indicates the outcome as

Example for Return Value:

public class Main {
  public static void main(String[] args) {
    String str1 = "Learn Java";
    String str2 = "Learn Java";

    // Comparing strings str1 and str2 (TechVidvan example)
    boolean result = str1.equals(str2);

    System.out.println(result);
  }
}

Output:
true

Example:

import java.lang.*;

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

        // Instantiate a String class
        String str = new String("Java");

        // Initialize the string object
        String obj = "Java";
        System.out.println("The given string is: " + str);
        System.out.println("The object is: " + obj);

        // Using the equals() method to compare strings (TechVidvan example)
        System.out.println("The given string is equal to the specified object or not? " + str.equals(obj));
    }
}

Output:
Java
Java
True

Difference between equals() vs equalsIgnoreCase() in Java:

Check whether a pair of strings are equal by using the equals() function in Java. In Java, to check for an equal representation of two strings that ignore a case, use equalsIgnoreCase().

So, let me give you two strings

String one =”qwerty”;
String two = “Qwerty”;

The two of them are identical, but the case is different. When the method excludes a case, it would consider both of these strings to be equivalent by means of equalsIgnoreCase().

Example:

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

       String one = "qwerty";
       String two = "Qwerty";
       if (one.equalsIgnoreCase(two)) {
    System.out.println("String 'one' is equal to 'two' ");
        }
else {
    System.out.println("String 'one' is not equal to 'two'");
         }

if (one.equals(two)) {
    System.out.println("String 'one' is equal to 'two' ");
        }
else {
    System.out.println("String 'one' is not equal to 'two' ");
        }
    }
}

Use of String equals():

public class
TechVidvan EqualsExample {  
    public static void main(String[] args)
{  
     String s1 = "TechVidvan";  
     String s2 = "TechVidvan";  
     String s3 = "TECHVIDVAN";  
     String s4 = "Java";  
     System.out.println(s1.equals(s2)); // true because content and case are the same  
     System.out.println(s1.equals(s3)); // false because case is not the same  
     System.out.println(s1.equals(s4)); // false because content is not the same  
    
}

Output:
True
False
False

Exception for Java Strings equals():

Advantages of Java Strings equals():

Technical details:

Disadvantage of Strings equals() in Java:

Conclusion

We have extensively discussed the equals() function in Java. We briefly discussed the use of the equals() method and then moved on to see how using this method can help reduce a significant amount of code in a program.

Exit mobile version