• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explain

 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

out put:
false
false
false

why explain me thanks

[ July 03, 2008: Message edited by: Chiru Raj ]
[ July 03, 2008: Message edited by: Chiru Raj ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer does not override the equals() method and so inherits the one in the Object class. Have a look at the documentation for Object.equals() and the output will make sense.
[ July 03, 2008: Message edited by: Joanne Neal ]
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Neal
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the reason the first comparison (s1==s2) returns false is that the == operator will, when used on objects, check if s1 and s2 are the same object. That is, if they are really references to the same memory area.
Had you written like this:

Then s1==s2 would have been true.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All three lines test object equality, not string equality. That's why they're mutually not equal.

Note that StringBuffer does not override the equals method, and thus uses the one inherited from Object (which tests for object equality). The String class, on the other hand, overrides equals to test for string equality.

Something like "s1.toString().equals(s2.toString())" might give you the result you were expecting.

PS: Which, as I now see, is just about what Joanne and Carl said. Oh well.
[ July 03, 2008: Message edited by: Ulf Dittmer ]
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

s1.toString().equals(s2.toString())

instead. You should get different results.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please don't use thread titles like "explain;" read this FAQ.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic