• 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

please explain the output, comparing with String

 
Greenhorn
Posts: 12
Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
if(sb1.equals(sb2)){
System.out.println("String Buffer False");
}
if (sb1 == sb2) {
System.out.println("False");
}
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just go through the documentation of StringBuffer class. You will find that it doens't override the equals method. So the equals method of Object class is called which uses == for comparing the objects.

Now two StringBuffer objects don't refer to the same object even if they contain the same value. Each StringBuffer object created through the new syntax will always point to a different object. So using == on them will return false...
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help greatly if you would also put in your post what the output IS, and what you EXPECTED it to be. That let's other focus in on what your misunderstanding is, rather than guess on what your confusion is.
    Bookmark Topic Watch Topic
  • New Topic