AppDividend
Latest Code Tutorials

Java String Equals Example | String equals() In Java

0

Java String Equals Example | String equals() In Java is today’s topic. In java, objects of strings are immutable, so that means we cannot once the string is created. Now, if we have two strings and we want to compare them if they are the same or not, then we can use the equals() method.

Java String Equals Example

The String equals() method compares the two given strings based on the content of a string.

If any character is not matched, then it returns false and if all characters are matched, it returns true.

  1. Java String equals() method overrides an Object class equals() method implementation.
  2. Java String is immutable, checking the equality of string to another object should be done using equals() method rather than == operator.
  3. String equals() method always returns a boolean value; it doesn’t throw any exceptions.
  4. The result of equals() method is true if and only if an argument is not null, it’s the String object, represents the same sequence of characters as this string.

See the following syntax.

string1.equals(string2);

Return Type:

String equals() method returns a boolean value: “true” if both the strings are same, otherwise “false”.

See the following code.

class equal
{
   public static void main(String args[])
   {
	String s1="AppDividend";
	String s2="appDividend";
	String s3="appdividend";
	String s4="AppDividend";

	//checking equality
	System.out.println(s1.equals(s2)); //return false
	System.out.println(s2.equals(s3)); //return false
	System.out.println(s1.equals(s4)); //return true
   }
}

See the following output.

 

Java String Equals Example | String equals() In Java

equalsIgnoreCase() Function in Java

Suppose you want to compare two strings with ignoring their case (upper or lower), then there is one sub-method called equalIgnoreCase(). All the properties are the same as equal() method has.

If you want the case insensitive equality check, then you can use a String equalsIgnoreCase() method. Its signature is public boolean equalsIgnoreCase(String anotherString), note that here the argument is a String object.

See the following code.

class equal {
	public static void main(String args[]) {
		String s1 = "AppDividend";
		String s2 = "appDividend";
		String s3 = "appdividend";
		String s4 = "AppDividend";

		// checking equality
		System.out.println(s1.equalsIgnoreCase(s2)); // return true
		System.out.println(s2.equalsIgnoreCase(s3)); // return true
		System.out.println(s1.equalsIgnoreCase(s4)); // return true
	}
}

See the output.

 

String equals() In Java

Java String equals() example with user input

Here is an instance where two strings are compared using the equals() and equalsIgnoreCase() methods. The user enters both of these string on command prompt, and we are using the Scanner class to read them. See the code.

import java.util.Scanner;

class Equal {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter First String:");
		String str1 = sc.nextLine();
		System.out.println("Enter Second String:");
		String str2 = sc.nextLine();
		sc.close();

		System.out.println("Both Strings are Equal? = " + str1.equals(str2));
		System.out.println("Both Strings are Case Insensitive Equal? = " + str1.equalsIgnoreCase(str2));
	}
}

See the output.

➜  java javac Equal.java
➜  java java Equal
Enter First String:
Ankit
Enter Second String:
Lathiya
Both Strings are Equal? = false
Both Strings are Case Insensitive Equal? = false
➜  java

Code snippet of equals() method

Below is the code snippet showing the implementation details of equals() method.

public boolean equals(Object anObject) {
    if (this == anObject) {
      return true;
    }
    if (anObject instanceof String) {
      String aString = (String) anObject;
      if (coder() == aString.coder()) {
        return isLatin1() ? StringLatin1.equals(value, aString.value) : StringUTF16.equals(value, aString.value);
      }
    }
    return false;
  }

Finally, Java String Equals Example | String equals() In Java is over.

Recommended Posts

Java String endsWith() Function Example

Java instanceof Operator Example

Java Immutable Class Tutorial

Java Finally Block Tutorial

Java String equalsIgnoreCase() Method

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.