• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuffer's toString and ==  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone


== gives true when both of its operand refer to same object
And for String equals() gives true if content of both String object are equal.


If String object occupies memory from String pool then their comparision
using== can give true.
here why the String produced by toString() don't take
memory from String pool?? As result of which == comparision gives false???

with regards
 
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a little read through The Ranch's FAQ. Specifically AvoidTheEqualityOperator

Also searching the Forum will bring up many many similar topics but here's one that I was involved in a while back that I found particularly enlightening.
 
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article is what you need: Strings, Literally
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String produced by the toString method is not a compile‑time constant.
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuilder f=new StringBuilder("ffff");

After executing this statement, you have two different objects, and only one of them is into the String pool.
 
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the toString() method for StringBuilder

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!