String equals( ) function in Java

In this tutorial, we will learn about the equals function in Java and its application in String variable fields.

So let’s start with the definition of equals function. String equals function is a method of Java that compares two Strings on the basis of the content of the String. If the character in the Strings compared is not matched in the same order then it returns false else it returns true.

Function Signature of equals function.

public boolean equals(Object str)

Methods to Use String Equals Function in Java.

Method 01: Simple Example

CODE:

class equalsfunction{
    public static void main(String[] args) {
        
        String x="Subhojeet";
        String y="Subhojeet";
        String z="sUbHoJeEt";
        System.out.println("Output for String x and y is -> "+x.equals(y));//return true
        System.out.println("Output for String z and y is -> "+z.equals(y));//return false
    }
}

OUTPUT:

Output for String x and y is -> true

Output for String z and y is -> false

 

The output for string x and y is true because the two string equal in at every position. Although string z and y return false because the equals function is also case sensitive and if both the strings have the same sequence of letters but different cases then it will eventually return to false.

Method 02: Level 2 Example

In this example, we will see how can we infuse if else condition along with equals function.

CODE:

class equalsfunction{
    public static void main(String[] args) {
        
        String x="Subhojeet";
        String y="Subhojeet";
        if(x.equals(y)){
            System.out.println("Equals Strings");
        }
        else{
            System.out.println("Unequals Strings");
        }
    }
}

OUTPUT:

Equals String.

Since the equals function returns a boolean value so it can easily fit inside a if else condition and return true and false for the particular two strings.

Method 03: Level 3

In this example we will use the application of the internal implementation of the String equals function:

CODE:

class equalsfunction{
    public static void main(String[] args) {
        
       String num="17";
       String decinum="17.43";
       String charx="S";
       Integer n=new Integer(17);
       Float d=new Float(17.43);
       Character c=new Character('S');

       //Reason is num is string type and the another is different object hence false

       System.out.println(num.equals(n));
       System.out.println(decinum.equals(d));
       System.out.println(charx.equals(c));

       //Reason is num is string type and the another is converted to String using tiString function.

       System.out.println(num.equals(n.toString()));
       System.out.println(decinum.equals(d.toString()));
       System.out.println(charx.equals(c.toString()));

    }
}

OUTPUT:

Note: equalsfunction.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

false

false

false

true

true

true

Hope this tutorial helped you to learn a lot about equals function and its application.

Leave a Reply

Your email address will not be published.