aspose file tools*
The moose likes Beginning Java and the fly likes explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "explain" Watch "explain" New topic
Author

explain

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 288


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 ]

-Chiru
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
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 ]

Joanne
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 288

thanks Neal
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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 ]

Ping & DNS - updated with new look and Ping home screen widget
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Try

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

instead. You should get different results.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
And please don't use thread titles like "explain;" read this FAQ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: explain
 
Similar Threads
meaningfully equivalent variables
About Strings and memory
== and equals
Strings, StringBuffer and equals()
string and string buffers