Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

stringbuffer equals

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following copied from poddar test.
1. StringBuffer sb1 = new StringBuffer("Amit");
2. StringBuffer sb2= new StringBuffer("Amit");
3. String ss1 = "Amit";
4. System.out.println(sb1==sb2);
5. System.out.println(sb1.equals(sb2));
6. System.out.println(sb1.equals(ss1));
7. System.out.println("Poddar".substring(3));
why line no.5 & 6 gives result false.
thanx in adv
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gopala,
StringBuffer class do not override equals() method. Hence the comparison is done not for the contents of the objects but for the object references and since these are two distinct objects with distinct references, the result is false.
The reason 6 is false for the same reason as mentioned above. Except ss1 is on pool and not on heap.
-Sandeep Nachane
------------------
Visit my homepage at
www.tipsmart.com
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic