• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

== and equals()

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain how these two works on string and stringBuffer classes and on objects and on strings.
thanks
sdev
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
Let me try to help u.
java.lang.Object Class contain
public boolean equals(Object obj) method and this class is the root of every class hierarchy.
So every class in java inherited this method. Every class should provide an appropriate implementation for the method otherwise that class would use the Object class version of the method. That simple check for two references and return true only if both the references are pointing to same object. Small example
public class Test{
public static void main(String arg[]){
Test a = new Test();
Test b = new Test();
System.out.println(a.equals(b)); //print false
a=b;
System.out.println(a.equals(b)); //print true
}
}
So for this case class Test is inherited from(implicitly) Object class and didn�t override the equals() method. So it is using Object class version of method.
First statement prints false because both references are pointing to different object.
Second statement prints true because both references are pointing to same object.
String class does provide a proper implementaion of equals() method and return true if the the content of both the String object is same or both the String Object is pointing to same object. Small example :
public class Test{
public static void main(String arg[]){
Test a = new String(�vivek�);
Test b = new String (�vivek�);
Test c = new String (�vivek1�);
System.out.println(a.equals(b)); //print true
System.out.println(c.equals(b)); //print false
b=c;
System.out.println(c.equals(b)); //print true
}
}

StringBuffer class does not provide a proper implementaion of equals() method and uses the Object class version and return false even the content of both variable is same because it is using Object class version of equals method which only check for two refrences not the content. It will return true only when both the refrences pointing to same object
public class Test{
public static void main(String arg[]){
Test a = new StringBuffer(�vivek�);
Test b = new StringBuffer(�vivek�);
System.out.println(a.equals(b)); //print false
b=a;
System.out.println(a.equals(b)); //print true
}
}
Hope this will help u.
java expert please feel free to correct me because I am trying to learn by giving answer of the question.
vivek

[This message has been edited by Vivek Shrivastava (edited July 13, 2000).]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shrivastava
That was a very lucid explanation of the below mentioned question. I just wanted to place my appreciation .
Thanks
Ravi

Originally posted by Vivek Shrivastava:
HI,
Let me try to help u.
java.lang.Object Class contain
public boolean equals(Object obj) method and this class is the root of every class hierarchy.
So every class in java inherited this method. Every class should provide an appropriate implementation for the method otherwise that class would use the Object class version of the method. That simple check for two references and return true only if both the references are pointing to same object. Small example
public class Test{
public static void main(String arg[]){
Test a = new Test();
Test b = new Test();
System.out.println(a.equals(b)); //print false
a=b;
System.out.println(a.equals(b)); //print true
}
}
So for this case class Test is inherited from(implicitly) Object class and didn�t override the equals() method. So it is using Object class version of method.
First statement prints false because both references are pointing to different object.
Second statement prints true because both references are pointing to different object.
String class does provide a proper implementaion of equals() method and return true if the the content of both the String object is same or both the String Object is pointing to same object. Small example :
public class Test{
public static void main(String arg[]){
Test a = new String(�vivek�);
Test b = new String (�vivek�);
Test c = new String (�vivek1�);
System.out.println(a.equals(b)); //print true
System.out.println(c.equals(b)); //print false
b=c;
System.out.println(c.equals(b)); //print true
}
}

StringBuffer class does not provide a proper implementaion of equals() method and uses the Object class version and return false even the content of both variable is same because it is using Object class version of equals method which only check for two refrences not the content. It will return true only when both the refrences pointing to same object
public class Test{
public static void main(String arg[]){
Test a = new StringBuffer(�vivek�);
Test b = new StringBuffer(�vivek�);
System.out.println(a.equals(b)); //print false
b=a;
System.out.println(a.equals(b)); //print true
}
}
Hope this will help u.
java expert please feel free to correct me because I am trying to learn by giving answer of the question.
vivek


 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you vivek. I have a small doubt in these two statements which u have written . First statement I got . Second one, I think it must be - Second statement prints true because both references are pointing to same object.
Am I right or wrong , please reply to me.

Thank You
sdev.
First statement prints false because both references are pointing to different object.
Second statement prints true because both references are pointing to different object
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
things to remamber for the Exam.
Object level is a shallow reference compare with ==
String level is a deap compare of object content
StringBuffer is a shallow reference compare with ==
Comparing a String to a StringBuffers always returns false
Wrapper classes for primative data types do a deap compare
within the same class. Otherwise the results is false.
Monty6
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes sdev,
U are right. that was a typo. i have changed it.
thanks
vivek
 
Bras cause cancer. And tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic