This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Software Teaming: A Mob Programming, Whole-Team Approach and have Woody Zuill and Kevin Meadows on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

StringBuffer

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
PLease look at the code below-
code:-
_________________________________________________________________
class Great
{
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("Hello");
StringBuffer sb1=new StringBuffer("Hello");
//String sb=new String("Hello");
//String sb1=new String("Hello");
System.out.println(sb.equals(sb1));
}
}
_________________________________________________________________
In the code above when I compare sb & sb1 as String the output I get is True, while as StringBuffer the output as false.
Can anyone tell me why is that so, as this is the case of deep checking, so I think in both cases the output should be true.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String class overrides the equals(Object o) method of
the Object class.
The StringBuffer class does not override the equals( Object o)
method.
So when you compare the string objects, it compares the value
In this case they both are equal, so it returns true.
In comparing StringBuffer objects, you compare the object
references in which case both being different you get
output as false.
Correct if wrong .
 
Devavrat Bagayat
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela thanks for your comments, now I got what exactly the reason was.
Thanks once again
reply
    Bookmark Topic Watch Topic
  • New Topic