Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

immutablity of strings  RSS feed

 
sree visu
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i have a doubt in the following code..I hope the answer is d but they have given it as "A"..please do clarify
 
Sergey Petunin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For all objects, including strings, the "==" operator checks the reference equality, so it returns true if the two variables refer to the same object in memory.

That's why s2 == s3 gives false: the s3 object is interned in the string pool at compile time, the s2 object is created at runtime.

Though s3 == s4 gives true, because due to the compile time optimization they refer to the same object interned in the pool of strings at compile time.
[ December 19, 2007: Message edited by: Serge Petunin ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!