equals() vs == in Java


There are two ways to determine if two objects are equal in Javal: the.equals() method and the == operator. The.equals() function compares the contents of two objects. The == operator compares the references of two objects.

When you create an object with the new operator, it gets assigned a specified memory location in the heap. Tak, for example, two objects are having the same data. Even if the two objects kept in different sections of memory, the.equals() method will return true.

The == operator returns true if two objects get stored in memory at the exact same location.

Differences between .equals() and ==

Aspect

.equals() Method

== Operator

Definition

The .equals() method is a part of the java.lang.Object class.

The == operator is a comparison operator in Java. It is applicable to both primitive data types and objects.

Comparison Focus

.equals() is employed to make comparison with the contents of objects. For instance, comparing the characters of two Strings or the numeric values of two Integers.

The == operator checks whether two objects reference the same memory location, thereby determining if they are the same object.

Behavior Customization

Custom behavior can be defined for the .equals() method in domain objects. For instance, custom criteria can be set for when two Employee objects are considered equal.

The behavior of the == operator cannot be altered.

Criteria Alteration

The .equals() method can be overridden to specify conditions for object equality.

The == operator's behavior remains static and unchangeable.

.equals() method

The String equals() function in Java evaluates two strings depending on their data/content. It returns true if all of the contents of both strings match. It returns false if none of the characters in question match.

.equals()

The code creates threads and string objects, then it compares them using == for references, resulting in false for all comparisons. It compares string content using .equals(), yielding true for strings with matching content.

Algorithm

  • Step 1: Generate a new thread instance named "t1."

  • Step 2: Create another new thread instance named "t2."

  • Step 3: Assign the reference of "t2" to the "t3" thread reference.

  • Step 4: Create two new String instances: "s1" and "s2." Alongside the content "Tutorials."

  • Step 5: With the usage of the "==" operator, see if "t1" and "t2" are of the same reference. Print the result (false) of this comparison.

  • Step 6: Again, compare if "t1" is the same reference as "t2" using the "==" operator. Print the result (false) of this comparison.

  • Step 7: The "==" operator gets used to check if the references "s1" and "s2" are the same. Print the result (false) of this comparison.

  • Step 8: Use the "equals" method to compare if "t1" is equal to itself. Print the result (true) of this comparison.

  • Step 9: Employ the "equals" method to compare if the content of "s1" is equal to the content of "s2." Print the result (true) of this comparison.

  • Step 10: End of the program.

Example

public class Tutorialspoint {
	public static void main(String[] args)
	{
		Thread t1 = new Thread();
		Thread t2 = new Thread();
		Thread t3 = t2;

		String s1 = new String("Tutorials");
		String s2 = new String("Tutorials");

		System.out.println(t1 == t2);
		System.out.println(t1 == t2);
		System.out.println(s1 == s2);

		System.out.println(t1.equals(t1));
		System.out.println(s1.equals(s2));
	}
}

Output

false
false
false
true
true

== operator

All primitive types, such as the boolean type, are subject to equality operators. For object types, equality operators are also applicable.

The output in the below code indicates that comparisons of distinct integer, character, and floating-point values return false. But comparisons of identical boolean values return true.

Algorithm

  • Step 1: Compare if the integer value 30 is equal to the integer value 40 with the help of the "==" operator. Print the result (false) of this comparison.

  • Step 2: See if the character 'x' is equal to the character 'y.” For that we use "==" operator. Print the result (false) of this comparison.

  • Step 3: Compare if the character 'x' is equal to the floating-point value 35.0 using the "==" operator. Print the result (false) of this comparison.

  • Step 4: Compare if the boolean value true is equal to the boolean value true using the "==" operator. Print the result (true) of this comparison.

  • Step 5: End of the program.

Example

public class Test {
	public static void main(String[] args)
	{
		
		System.out.println(30 == 40);

	
		System.out.println('x' == 'y');

	
		System.out.println('x' == 35.0);

	
		System.out.println(true == true);
	}
}

Output

false
false
false
true

Conclusion

The.equals() method compares contents of two objects. On the other hand, the == operator compares objects' memory addresses. We have looked at an example for each method to understand the distinctions.

Updated on: 29-Aug-2023

375 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements