| Author |
== and equals question required
|
PETER CARTER
Ranch Hand
Joined: Aug 28, 2004
Posts: 70
|
|
1: public class Myclass { int x; Myclass(int i){x = i;} public static void main(String args[]) { Myclass m1 = new Myclass(100); Myclass m2 = new Myclass(100); System.out.println(m1.equals(m2)); } } The result is :false . Why ? I think it should be :true. 2: public class test{ public static void main(String[] a){ String s1 = "amit"; String s3 = "arit"; String s4 = "arit"; String s2= s1.replace('m','r'); System.out.println(s2 == s3); System.out.println(s3 == s4); } } The answer is : false true But I think it should be :false false. Thanks !!
|
 |
Nandish KB
Greenhorn
Joined: Aug 18, 2004
Posts: 6
|
|
Hi PETER, 1. Unless u override the equals() method, it will just compares the object references. here in SOP(System.out.println) the Object class' equals() method is called. As u r creating new 2 references, it will compare the BITS(internal representation , nothing but address(long integer)). So they are differnt.It will result in false. 2. When u call String s1 = "amit"; it will internally calls like this, String s1= new String("amit"); JVM's String handling is like that, while creating Strings, for OPTIMIZATION, it will point the 2 references to the same object whose contents are same.But in S2==S3 case, this is somewhat different, as you are calling replace()method, The String.replace method will create a new object, As I think at this stage it wont look for optimization. Correct me If I am Wrong, Regards, Nandish
|
Regards,<br />Nandish
|
 |
Mahesh Bhatt
Ranch Hand
Joined: Sep 15, 2004
Posts: 88
|
|
Hi there, Whenever you use the "==" , it checks whether both the variables point to the same memory addres. However when you use "equals" it checks for the contents of the objects . However in case of strings they act a little bit differently, why ? I would suggest you to write small code to check that, it would clear all your doubts. try these two (equals and ==) with strings and objects, compare the outputs and you would see that you have cleared all your doubts, (and once you learn them yourself, I bet you would never forget them ).
|
Impossible is I M Possible
|
 |
Louie van Bommel
Ranch Hand
Joined: Aug 17, 2004
Posts: 76
|
|
Originally posted by Nandish KB: Hi PETER, 1. ...just compares the object references. ... nothing but address(long integer)). ... 2. ...while creating Strings, for OPTIMIZATION, it will point the 2 references to the same object whose contents are same.But in S2==S3 case, this is somewhat different, as you are calling replace()method, The String.replace method will create a new object, As I think at this stage it wont look for optimization.
Pretty much. A few notes. The equals() method of Object is being used if you create a subclass of Object and don't override the method yourself; you will therefore get results that are precisely the same as == There's no indication in java as what exactly a reference is. (It's not a long integer). 2. If you create Strings using literals s="hello", the literal goes into a literal pool. Any other literals being assigened in the program also go in that pool. If there is a 2nd literal that's the same as any other literal in the pool, the reference points to the same address.
|
 |
 |
|
|
subject: == and equals question required
|
|
|