Equals and equalsIgnoreCase Example Java Program


Definition

One of the String comparison methods is '.equals' method. This method is case-sensitive and returns true only when both the strings that are compared are equal in case. 'equalsIgnoreCase' method is case-insensitive. This method returns true if both the strings that are compared are equal irrespective of the case.

Syntax

val1.equals(val2)
val1.equalsIgnoreCase(val2)

Equals and equalsIgnoreCase Example Program

class StringComparison{
	public static void main(String[] args){
		String str1 = "Java";
		String str2 = "JAVA";
		System.out.println("Comparing strings : "+str1 + " and "+str2);
		System.out.println(str1 + " equals "+str2+" : "+str1.equals(str2));
		System.out.println(str1 + " equalsIgnoreCase "+str2+" : "+str1.equalsIgnoreCase(str2));
	}
}

Sample Output

Comparing strings : Java and JAVA
Java equals JAVA : false
Java equalsIgnoreCase JAVA : true