Difference between == and equals() method in Java

HOME

What is == operator ?

To compare primitives and objects we use “==” equality operator in java which is a binary operator given in the java programming language

What is equals() method in java?

The Java String class equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.

Difference between == and equals method in java

1. == is an operator whereas equals() is a method of Object class.

2. == operator compares reference or memory location of objects in the heap, whether they point to the same location or not. Whenever we create an object using the operator new it will create a new memory location for that object. So we use the == operator to check memory location or address of two objects are the same or not.
equals() method  is used to compare the state of two objects or the contents of the objects.

3. Equals method can be overridden but you can’t override behavior of “==” operator.

Below is an example which shows the use of == and equals.

public class EqualsDemo1 {

	public static void main(String[] args) {
		String str1 = new String("JAVATUTORIAL");
		String str2 = new String("JAVATUTORIAL");

		// Reference comparison
		System.out.println("Check str1 == str2 : " + (str1 == str2));

		// Content comparison
		System.out.println("Check str1.equals(str2) : " + str1.equals(str2));

		// integer-type
		System.out.println("Check Integer Type :" + (10 == 10));

		// char-type
		System.out.println("Check Char Type :" + ('a' == 'a'));
	}
}

Output
Check str1 == str2 : false
Check str1.equals(str2) : true
Check Integer Type :true
Check Char Type :true

Let us take an example where both strings have same characters and same length but in lower case.

public class EqualsDemo2 {

	public static void main(String[] args) {
		String str1 = new String("JAVATUTORIAL");
		String str2 = new String("jAVATUTORIAL");

		// Reference comparison
		System.out.println("Check str1 == str2 : " + (str1 == str2));

		// Content comparison
		System.out.println("Check str1.equals(str2) : " + str1.equals(str2));
	}
}

Output
Check str1 == str2 : false
Check str1.equals(str2) : false

Let us take an example where both Integer refer to the same object.

public class EqualsDemo3 {

	public static void main(String[] args) {

		Integer i = new Integer(10);
		Integer j = i;

		System.out.println("Check i == j : " + (i == j));
}

Output
Check i == j : true

Let us take an example where both Integer1 and Integer2 refer to the different objects.

public class EqualsDemo4 {

	public static void main(String[] args) {

		Integer i = new Integer(10);
		Integer j = new Integer(10);

		System.out.println("Check i == j : " + (i == j));
	}
}

Output
Check i == j : false

That’s it for today. Have a wonderful learning.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s