• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java doubt about ==

 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Operator_test{
String one,two;
}
class Main2{
public static void main(String ar[])
{
Operator_test test=new Operator_test();
Operator_test test2=test;
Operator_test test1=new Operator_test();
test.one="XYZ";
test1.two="XYZ";
if(test==test2)
System.out.println("test and test2 are equal");
else
System.out.println("test and test2 are not equal");
}
}
Hi ,
I was just checking the function of == operator and wrote the above code .The thing i am confused is that
i am getting the output as "test and test2 are equal " ,== operator comapres the reference variabels test and test2 (and the not the content ,"XYZ") which are not equal so why if condition is passing .
Thanks in advance
 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


As you can see the comments in above code. The if statement you are doing is "test" and "test2" which point to the SAME reference in memory.

However if you do "if (test==test1)" then the else part will print. Try it
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals get put into the String pool. so, "XYZ" is an object in the String pool.

test.one="XYZ";
test1.two="XYZ";

sets both reference variables pointing to that same object, so test.one and test1.two really do have the same value.
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:

As you can see the comments in above code. The if statement you are doing is "test" and "test2" which point to the SAME reference in memory.

However if you do "if (test==test1)" then the else part will print. Try it


Thanks for your reply one more issue

class Operator_test{
String one,two;
}
class Main2{
public static void main(String ar[])
{
Operator_test test=new Operator_test(); // first instance
Operator_test test2=test; // same instance
Operator_test test1=new Operator_test(); // second instance
test.one="XYZ";
test1.two="XYZ";
if(test.equals(test1))
System.out.println("test and test1 are equal");
else
System.out.println("test and test1 are not equal");
}
}
As equals method check the contents so test and test1 object should be equal and output should be "test and test1 are equal " but if condition is failing and i am getting else part as output.
Thanks again
 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
upanshu vaid wrote:
As equals method check the contents so test and test1 object should be equal and output should be "test and test1 are equal " but if condition is failing and i am getting else part as output.
Thanks again


test and test1 are objects of Operator_test. These are objects not strings. The Object class equals() method behaves the same as == operator if the equals() method is NOT overridden.

In another words: if equals() is not overridden, it defaults to ==; that is, it returns true if both variables refer to the same object (in memory)
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
upanshu vaid,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic