• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reversing String Not Working...

 
Somnath Mallick
Ranch Hand
Posts: 482
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Somnath Mallick
Ranch Hand
Posts: 482
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object's equals method below



== check whether two references point to the same Object or not
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 482
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 482
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh man!! You must be a very good tutor.


Yeah! Agree with that part!!!
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this post closed..?
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to compare StringBuilders or StringBuffers on content, call toString() on each and compare those:
 
Somnath Mallick
Ranch Hand
Posts: 482
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! That's what i finally did! Thanks everyone for their comments!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic