• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain the output, comparing with String

 
kumar Nagesh
Greenhorn
Posts: 12
Tomcat Server
  • 0
  • 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");
}
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • 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...
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12024
25
Chrome Java Linux
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic