• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equality in string builder

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys, a quick question about this code (originally taken from Mock exam 2 http://sybextestbanks.wiley.com/ )


I'm OK with all of them except the last one: why isn't this true?
thanks
 
Henry Wong
author
Marshal
Pie
Posts: 22092
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:
I'm OK with all of them except the last one: why isn't this true?


The StringBuilder class does not override the equals() method. I am not completely sure why, but will speculate that it was only intended to be used in a way to build strings. [Oops. I misread the question.]

Why should that statement be true? The StringBuilder class has to create a string to represent what was built so far, and it certainly don't need to use the string pool for that.

Henry
 
Liutauras Vilda
Bartender
Pie
Posts: 2773
111
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because s1 not sitting in a string constant pool, and more precisely, toString() method being invoked on a runtime, so new object being created while "meow" sitting in a string constant pool. And since '==' checking if two references referring to the same object in a memory location, this gives you a false.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The StringBuilder class does not override the equals() method

Thanks, but this is  == not equals()?
Also, re-reading the question again, it looks like I have misinterpreted it... when we say we obviously mean that s1 and meow are effectively the same object, so whether the value of s1 is stringified (meaning it has toString() applied to it) or not, it will still, of course, return false...
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:when we say we obviously mean that s1 and meow are effectively the same object, so whether the value of s1 is stringified (meaning it has toString() applied to it) or not,

s1 and "meow" are not the same object. s1 is an object of StringBuilder having value meow and String literal "meow" causes creation of an object of String type having value "meow" whose reference is stored in String literal pool because this String object is created by String literal.

When you apply toString() method of StringBuilder i.e. s1.toString() It creates new object of String by using new operator having value "meow"  and returns It, so the String object returned by this method is different than the String object which was created by String literal "meow" although both have same value.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:we obviously mean that s1 and meow are effectively the same object

That's completely wrong! s1 is a StringBuilder reference variable and "meow" is a String literal. So it's simply impossible that both are the same object.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry I obviously didn't express myself correctly. When I said

we obviously mean that s1 and meow are effectively the same object,[/code] I mean that for that to be true the string meow and the StringBuilder had to be the same object, but because that's not the case, it returns false.
 
Liutauras Vilda
Bartender
Pie
Posts: 2773
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would do no harm if you'd re-read that chapter about the Strings. Not sure which book you own, but please re-read once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic