996 points
8 4 3


Hello, the good people

I am struggling with confusion for the last hour.
What is the basic and main difference between the (==) and .equal() in java? Can I use the operator == to check whether two variables refer to the same array?

If it is possible, please give me an example. Both usages of them are much appreciated in a single program.




Let’s understand what is the == operator and .equal() in java first.
Generally, both the == and .equal() use for comparison of objects equality, but they still have some differences.
The very first inequality of them is, the == is an operator and the .equal() is a function. The .equal() function
is used for the comparison of the values of the object,
but the other one is used for checking the memory location of both objects.

Let’s write a code using both the operator and function in a single program as you requested above:

public class TestClass { 
    public static void main(String[] args) 
        String str1 = new String("I am Gavin"); 
        String str2 = new String("I am Gavin"); 
        System.out.println(str1 == str2); 

This program will produce the output :


Because we created two string objects str1 and str2 assigned both of them with the same value. As we know the == operator checks the memory location of two objects and .equals() check the values of two objects.

I hope you get the point. Thanks. Keep coding.

More Posts

What is the difference between public, protected, package-private and private in Java? Alex Samson - Apr 6, 2020
What is the difference between java and core java Sanjana Sagar - May 28
What issues should be considered when overriding the equals and hashCode methods in Java? Alex Samson - Apr 6, 2020
what will be the values of x and y as a result of the following code? int x = 25, y = 8; x += y++; SharadMagar450 - Aug 12
Difference between is and == in Python Prasandeep Gautam - Nov 6, 2020
Difference between equals() and "==" string comparison - Java DivyaN - Sep 12
What does >> do in java. Tania - Sep 16
What does += mean in java Hasnain_khan - Oct 9, 2020
what is the difference between an arguments and parameters Being human - Jun 5
what is the difference between an arguments and parameters Being human - Jun 5