Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with String class

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can someone explain the reason for the two different outputs?what difference does s3 have because s2 is final?
thanks.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Question.....!!!

Same Doubt forwarded to others !!!
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got me.

Would really appreciate if someone can draw how this looks in memory, esp when s2 is final vs. non-final.
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandra Bachan wrote:
Would really appreciate if someone can draw how this looks in memory, esp when s2 is final vs. non-final.


It's not a memory issue. It's a compiler issue.

A final string referenced that is assigned during declaration to a string literal is a compile time constant. So... The s2 reference is a compile time constant. The sum of two compile time constants is a compile time constant. Hence, the assignment of the s3 reference is to a string that is a compile time constant, and hence, in the string pool. So... the first comparison is true.

While the s4 reference is pointing to a string that is in the string pool, the concat being done to be assigned the s5 reference is not done at compile time, and is not in the string pool. And hence... the second comparison is false.

Henry



 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Henry

but at runtime isnt it the same String literal "abc" that lies in the String pool that both s5 and s3 are pointing to?
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line no 7 should be +"c" instead of "a", though the output will still be true.

but at run time isn't it the same String literal "abc" that lies in the String pool that both s5 and s3 are pointing to?


I infer if computation has one non final literal, the string pool is not used.

example :- String s5=s4+"c"; will not use string pool and print false, where as String s5="ab"+"c"; will use pool.

-P

 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevinn lee wrote:
but at runtime isnt it the same String literal "abc" that lies in the String pool that both s5 and s3 are pointing to?


No. As already explained, s5 is calculated at runtime, and is not in the string pool (whereas s3 is merely assigned at runtime and in the string pool).

Henry
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be missing something but did not understand it fully
Henry sir said that the addition of two compile time constants is a compile time constant
here
s2 is a compile time constant and not the literal "C"
then how can s2 + "C" be the compile time constant?
doesn't the addition s2+"C" occur at runtime?
how does s3 become a compile time constant?
s3 = s2 + "C"
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Kharkar wrote:
here
s2 is a compile time constant and not the literal "C"
then how can s2 + "C" be the compile time constant?


String literals are, of course, compile time constants.

Prasad Kharkar wrote:
doesn't the addition s2+"C" occur at runtime?


No. The sum of two compile time constants is done at compile time -- which yields another compile time constant.

Prasad Kharkar wrote:
how does s3 become a compile time constant?
s3 = s2 + "C"


s3 is *not* a compile time constant. It is merely a reference that is assigned to an object that is a compile time constant. Hence, it is a reference to an object in the string pool.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic