• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding equals() for StringBuilder  RSS feed

 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wrote a piece of code which would add objects to a Set. I have overridden equals() and hashcode() ,so that no duplicate object gets added in the Set.

Following is the object i am adding to the Set



Here is the main method




The problem is even though the contents of the StringBuilder of both the objects are same i.e. meaningfully equal yet the overriden equals() fails and the Set contains the duplicate object !

Following is where i add the object to the Set.


When i tweak the code as



i.e. after adding the equals() works absolutely fine !
Can anyone please explain?

Thanks,
Adithya.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, StringBuilder does not override equals(), so ((MessageAndId)obj).getMessage().equals(this.getMessage()) will never be true in your case. Is there any reason why you are using a StringBuilder instead of a String ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's also why it works by adding toString(), because the String class overrides equals().
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is there any reason why you are using a StringBuilder instead of a String ?


Well,i didn't want the string variables sitting inside the string pool ! Hence,i opted for StringBuilder.

Unfortunately, StringBuilder does not override equals(), so ((MessageAndId)obj).getMessage().equals(this.getMessage()) will never be true in your case.

Looks like i will have to reert back to String instead of StringBuilder or while comparing in equals() convert the StringBuilder to String using toString() , but that won't make sense if i will have to ultimately create a String (toString() method will create a new String right or just create a literal and place it in the pool) ! Am i right ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does not make sense here is to use a StringBuilder instead of a String What are you afraid of exactly ?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adithya narayan wrote:
Is there any reason why you are using a StringBuilder instead of a String ?


Well,i didn't want the string variables sitting inside the string pool ! Hence,i opted for StringBuilder.

Not an issue. Strings created by StringBuilder aren't in any pool, unless you call the intern() method. More generally, strings aren't in the string pool unless (a) they appear as String literals or constant expressions in the code, or (b) you call the intern() method to put the string in the pool. Strings created any other way are just ordinary objects.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote

Not an issue. Strings created by StringBuilder aren't in any pool, unless you call the intern() method. More generally, strings aren't in the string pool unless (a) they appear as String literals or constant expressions in the code, or (b) you call the intern() method to put the string in the pool. Strings created any other way are just ordinary object


So,what does it mean if i use the following:


After calling the toString() the string isn't added in the pool ??

Adithya.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote :

Not an issue. Strings created by StringBuilder aren't in any pool, unless you call the intern() method. More generally, strings aren't in the string pool unless (a) they appear as String literals or constant expressions in the code, or (b) you call the intern() method to put the string in the pool. Strings created any other way are just ordinary objects.


What all happens when i say the following :



Adithya.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote :

What does not make sense here is to use a StringBuilder instead of a String What are you afraid of exactly ?


I was just avoiding the string to hog the string pool memory.
If i would have said


it would have created a string in the heap as well as in the string pool which isn't the case when one uses StringBuilder.

Adithya.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String "adithya" is added to the String pool, because it appears in your source code as a String literal.

This example won't add any String to the pool:
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh..So,does the string in the pool also get modified if i change the literal.
For e.g.


Now,how many strings are there in the pool and in the heap allocated to the JVM ?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adithya narayan wrote:Oh..So,does the string in the pool also get modified if i change the literal.
For e.g.


Now,how many strings are there in the pool and in the heap allocated to the JVM ?

There are two strings in the pool, "adithya" and " narayan". At this point, there are no additional strings in the heap - not from this code, anyway. There's also a StringBuilder on the heap, with internal contents "adithya narayan", but it's not a String. Yet.

If we add one more line:

Now there are still the same two strings in the pool, and there is one new string on the heap, "adithya narayan". And the StringBuilder is still on the heap as well.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adithya narayan wrote:If i would have said


it would have created a string in the heap as well as in the string pool which isn't the case when one uses StringBuilder.

Well, the first line puts a string in the pool and a string in the heap, two identical objects, pointless duplicates. The second put a string in the pool and a StringBuilder in the heap. Two different objects representing the same content, again pointless. If instead you jest have

then you just have one string in the pool, with no duplicated content elsewhere. This is almost certainly better than the other two.

You're putting a lot of energy into not having anything in the string pool, and it's not working, because you keep getting things in the string pool. Why bother? Just let strings exist in the string pool. That's what it's designed for. Not all strings end up there - but many do. And that's all right.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adithya narayan wrote:
Mike Simmons wrote

Not an issue. Strings created by StringBuilder aren't in any pool, unless you call the intern() method. More generally, strings aren't in the string pool unless (a) they appear as String literals or constant expressions in the code, or (b) you call the intern() method to put the string in the pool. Strings created any other way are just ordinary object


So,what does it mean if i use the following:


After calling the toString() the string isn't added in the pool ??

Adithya.

No, it's not.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for giving me such patient replies. I keep coming back here and i return with more knowledge. Thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!