• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue with String class

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Question.....!!!

Same Doubt forwarded to others !!!
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic