aspose file tools*
The moose likes Java in General and the fly likes Java doubt about == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java doubt about ==" Watch "Java doubt about ==" New topic
Author

Java doubt about ==

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

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

Joined: Sep 13, 2007
Posts: 2531
    
    8



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


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

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

Joined: Sep 13, 2007
Posts: 2531
    
    8

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

Joined: Apr 06, 2010
Posts: 4422
    
    8

upanshu vaid,
Your post was moved to a new topic.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Java doubt about ==