Difference Between equals() and == in Java

The equals() method and == administrator in Java perform two distinct activities. The equals() method

is utilized to analyze two string esteems. Then again == administrator is utilized to contrast two crude datatype factors or with thinking about references of two objects.

The following is an example program that shows contrast among equals() and == in Java.

class EqualsAndEqualExample
{
	public static void main(String...s)
	{
		String str1=new String("JustTechReview");
		String str2=new String("JustTechReview");
		
		System.out.println("str1 equals() str2 "+str1.equals(str2));
		System.out.println("str1 == str2 "+(str1==str2));
	}		
}

Output:

Difference Between equals() and == in Java

In the first case, we are looking at the qualities or substance of String objects str1 and str2 utilizing equals() method.

The outcome is genuine in light of the fact that their values are equivalent.

In the second case, we are looking at the references of String objects str1 and str2 utilizing == administrator.

The outcome is bogus in light of the fact that their references are not risen to as both the objects are unique.

Leave a Comment