• Register
1 vote
42 views

Problem:

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.

Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

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); 
        System.out.println(str1.equals(str2)); 
    } 
}

This program will produce the output :

false
true

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,240 points

Related questions

0 votes
1 answer 18 views
0 votes
1 answer 21 views
21 views
Explain in context of strings in Java?
asked Sep 20, 2020 jagruthi 680 points
0 votes
2 answers 19 views
19 views
Solve this.
asked Jan 24 TeamScript 13.5k points
0 votes
1 answer 5 views
5 views
The problem is that I do not know the difference between them, and I want a solution. Please give an example so that I can understand. Thank you
asked Feb 5 ahmedadel9090 2.6k points
0 votes
1 answer 7 views
7 views
Problem: Hello! I want to learn to program. I have no previous experience with programming or computers. I discussed with some experienced programmers about which language to learn. Some of them suggested starting with Java, while others suggested C++, and ... and JavaScript. Could anybody here tell me the difference between both? Moreover, what if I start learning programming from JavaScript?
asked Jan 12 Code Learner 6k points
0 votes
1 answer 24 views
24 views
Problem: Hi there! I want to know the difference between the Collections and Collection in Java. What is the main difference between both, and can they be used interchangeably? Please answer in detail. Thanks
asked Dec 29, 2020 Code Learner 6k points
0 votes
1 answer 13 views
13 views asked Oct 28, 2020 miki 1.9k points
0 votes
1 answer 17 views
0 votes
1 answer 12 views
0 votes
1 answer 41 views
41 views
I am new to this. I just want to know how to do that? Solve this problem.
asked Nov 6, 2020 TeamScript 13.5k points