• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

String a-go-go

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Java Gurus! I'm back at it with a question from reading the Java Language Specification 3.10.5: String literals. Heres a code snippet from this topic (perhaps you have seen this at one time or another):


LINE A prints "true", LINE B prints "false".

Now the Java spec states the following (among others):
Quote 1:

Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.


and:
Quote 2:

Strings computed by concatenation at run time are newly created and therefore distinct.



My confusion is this: Is Quote 1 referring to LINE A in the code? I'm thinking that "Hel" and "lo" are the constant expressions the spec is referring to. Am I correct?
And: Is Quote 2 referring to LINE B in the code? Is "Hel" in LINE B newly created during run time because its concatenated to lo? If so, why isn't the same thing true for "Hel" and "lo" on LINE A?

Please straighten my tangled strings.
Thanks much.
g
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding is that, in Line A, "Hel" and "lo" are string literal(or string constant), so "Hel"+"lo" is the same as you write "Hello". However, in Line B, "Hel" is still a string literal(or string constant) but lo is a string reference and its value is not determined at compile time so the value of ("Hel" + lo) is not determined at compile time but run time. Therefore, a new string is created for "Hel" + lo.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember String Objects are final but not references until unless explicitly specifed.

So in the code snippet,

"lo" --> String Object created and maintained in the String pool

String lo = "lo" --> lo is a reference variable that points to String Object.lo variable can be altered to point to a different object as is not a constant but you can treat "lo" as constant(Though the terminology is used wrongly for an object, just to memorize)


If you would had declared the variable as final String lo ="lo" then

"Hello" == "Hel" + lo would be true.

or Hello == ("Hel"+lo).intern() would be true.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm taking a little risk here, because I'm not 100% sure, but I'm pretty sure (maybe 98%?), that you guys are going deeper into this topic than you will need to for the exam. So, my advice would be to turn your attention to other exam topics until you are sure you have them absolutely nailed. Then, if you're still hungry for more Java knowledge, you could come back and tackle this issue.

reply
    Bookmark Topic Watch Topic
  • New Topic