• Register
1 vote


Hello, the good people of Kodlogs,

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.


12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
Best answer


Hello Gavin,

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 object 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.

13 9 6
94,260 points

Related questions

0 votes
1 answer 12 views
Problem: Hi there! I am new to Java. I want to know the difference between the == operator and .equal() method. When do we use each and what difference do they make if used interchangeably? Thank you!
asked Mar 29 Code Learner 9.8k points
0 votes
1 answer 19 views
0 votes
1 answer 28 views
Explain in context of strings in Java?
asked Sep 20, 2020 jagruthi 680 points
0 votes
1 answer 7 views
Problem: How could I get the answer of what significant justification is there for the -> operator in c and c++?
asked Apr 9 ashik 14.6k points
0 votes
1 answer 4 views
Problem: what is || operator and how does it function in a program?
asked Apr 13 nabin shrestha 4.3k points
0 votes
1 answer 13 views
If you can help with any example then plsease share.
asked Sep 25, 2020 okeke 360 points
0 votes
1 answer 17 views
I need some to help me explain what operators are.
asked Sep 25, 2020 okeke 360 points
0 votes
1 answer 6 views
Help! I need someone solving this>and operator in shell script if condition
asked Apr 11 ashik 14.6k points
0 votes
1 answer 4 views
Problem: What are my options for dealing with this issue? most significant bit and least significant bit
asked Apr 9 rakib1 51.5k points
0 votes
1 answer 8 views
Problem: Why I am getting the following error: $ operator is invalid for atomic vectors?
asked Apr 3 ashik 14.6k points