exampracticeonline.com
String equals()

Java String equals()

The Java String equals() method checks two String equailty based on the content of both String. If both String have same charaters then equals() method returns true else returns false.

Important points:

  1. Java String equals() method is a case sensitive. Java has equalsIgnoreCase() method for case insensitive comparision.
  2. String class overrides equals() method from Object class.
  3. "==" operator compare the references but equals() method compare contant of given String.

Internal method implementation

You can see this internal implementation in String.class provided in java.lang package.

/**
     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     *
     * @param  anObject
     *         The object to compare this {@code String} against
     *
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     *
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
     */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

Method Syntax

public boolean equals(Object anObject)  

Method Parameter

anObject: another object that compared with given String.

Method Returns

Java String equals() method returns boolean value true or false. true if characters of both strings are equal otherwise false.

Method Overrides

String class overrides equals() method from Object class.

1. Example - Java String equals() method

package com.examples;

public class StringEqualsExample {
	
	public static void main(String args[]) {
		String str1 = "java";
		String str2 = "JAVA";
		String str3 = "java";
		String str4 = "python";
		System.out.println("Is java and JAVA equals ? : "+str1.equals(str2));
		System.out.println("Is java and java equals ? : "+str1.equals(str3));
		System.out.println("Is java and python equals ? : "+str1.equals(str4));
	}
	
}

Output:

Is java and JAVA equals ? : false
Is java and java equals ? : true
Is java and python equals ? : false

2. Example - Java String equals() method

package com.examples;

public class StringEqualsExample {
        
        public static void main(String args[]) {
                String str = "Java learning";
                if(str.equals("Java learning")) {
                        System.out.println("Yes, str equals with Java learning");
                } else {
                        System.out.println("No, str equals with Java learning");
                }
        }     
}

Output: 

Yes, str equals with Java learning

3. Example - Java String equals() method

package com.examples;

public class StringEqualsExample {
        
        public static void main(String args[]) {
                String str = "Java learning";
                if(str.equals("JAVA learning")) {
                        System.out.println("Yes, str equals with JAVA learning");
                } else {
                        System.out.println("No, str not equals with JAVA learning");
                }
        }     
}

Output:

No, str not equals with JAVA learning