aspose file tools
The moose likes Beginning Java and the fly likes explain Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "explain" Watch "explain" New topic
Author

explain

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 283


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: 3011
    
    9
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: 283

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: 35237
    
    7
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 ]

Android appsImageJ pluginsJava web charts
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
Try

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

instead. You should get different results.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
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
== and equals
About Strings and memory
Strings, StringBuffer and equals()
string and string buffers
meaningfully equivalent variables