• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have the follow code:
...
...
String s = "hi";
String ss = new String("hi");
StringBuffer sb = new StringBuffer("hi");
s.equals(ss) -------> true
s.equals(sb) -------> false
Why??
Thak you in advance.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we see the Java documentation. Then both the classes are extending Object class and they have no relationship with each other excpet that they are both the members of same package. And both are used to represent strings. String class is used to represent immutable strings while string buffer class is used to represent mutable strings. If you call the toString method of the StringBuffer class and then pass the resulting object to equals method. Then it also returns true.
String s = "hi";
String ss = new String("hi");
StringBuffer sb = new StringBuffer("hi");
s.equals(ss) -------> true
s.equals(sb.toString()) -------> true
Regards
Muhammad Ali
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String API doc says the following:
Equals() Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
This means the object you are comparing to should be a string object.

Originally posted by Jordi Marqu�s:
Hi!
I have the follow code:
...
...
String s = "hi";
String ss = new String("hi");
StringBuffer sb = new StringBuffer("hi");
s.equals(ss) -------> true
s.equals(sb) -------> false
Why??
Thak you in advance.


 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String Class overrides the equals method from the object class and StringBuffer doesn't. so you are comparing two different object methods. I hope this would help.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic