| Author |
Reversing String Not Working...
|
Somnath Mallick
Ranch Hand
Joined: Mar 04, 2009
Posts: 471
|
|
Hi Everyone,
This is giving the output as false! Could you please tell me why? I dont think there is anything wrong with the code!
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
I dont think there is anything wrong with the code!
Think twice. Does StringBuilder override the Object#equals method ? If no, what are the consequences ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Somnath Mallick
Ranch Hand
Joined: Mar 04, 2009
Posts: 471
|
|
So could you please tell me how to check it? I checked the Java API for the StringBuilder class and I found that there is mention of:
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Object's equals method below
== check whether two references point to the same Object or not
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
So could you please tell me how to check it?
That's a good opportunity to learn how to read the API. Check the java.lang.StringBuilder API again. In the "Method Summary", you'll see all the methods implemented in StringBuilder. These can be StringBuilder's own methods, or some overriden methods. The equals method is not in there, so StringBuilder does not override it. You can see in the "Methods inherited from class java.lang.Object" part that it actually inherits the equals method from Object. Now, look at the API for java.lang.Object#equals(). What does it tell you ?
Furthermore, if you take a look at the java.lang.String API, you'll see that in the "Method Summary", the equals method is there. It's overridden, and it does something special, that StringBuilder does not. Try to find out what.
|
 |
Somnath Mallick
Ranch Hand
Joined: Mar 04, 2009
Posts: 471
|
|
|
Ok! Thanks Christophe! So does this mean that the check in the above code is not is that the objects are pointing to the same location or not and not the contents of the objects?
|
 |
nandini lagunia
Ranch Hand
Joined: May 05, 2009
Posts: 57
|
|
Christophe Verré wrote:
So could you please tell me how to check it?
That's a good opportunity to learn how to read the API. Check the java.lang.StringBuilder API again. In the "Method Summary", you'll see all the methods implemented in StringBuilder. These can be StringBuilder's own methods, or some overriden methods. The equals method is not in there, so StringBuilder does not override it. You can see in the "Methods inherited from class java.lang.Object" part that it actually inherits the equals method from Object. Now, look at the API for java.lang.Object#equals(). What does it tell you ?
Furthermore, if you take a look at the java.lang.String API, you'll see that in the "Method Summary", the equals method is there. It's overridden, and it does something special, that StringBuilder does not. Try to find out what.
Oh man!! You must be a very good tutor.
|
 |
Somnath Mallick
Ranch Hand
Joined: Mar 04, 2009
Posts: 471
|
|
Oh man!! You must be a very good tutor.
Yeah! Agree with that part!!!
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
So does this mean that the check in the above code is not is that the objects are pointing to the same location or not and not the contents of the objects?
Yes, you're right, the equals() implementation of the Object class compares references:
API wrote:The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
So, in your case, sb.equals(sbr) compares both StringBuilder's reference, not their content.
|
 |
netali joshef
Greenhorn
Joined: Mar 27, 2009
Posts: 12
|
|
|
Is this post closed..?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
If you need to compare StringBuilders or StringBuffers on content, call toString() on each and compare those:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Somnath Mallick
Ranch Hand
Joined: Mar 04, 2009
Posts: 471
|
|
Yes! That's what i finally did! Thanks everyone for their comments!
|
 |
 |
|
|
subject: Reversing String Not Working...
|
|
|