• 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

String literals and garbage collection

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I truly hope this explanation puts the "If String literals are objects, are they garbage collected?" issue to rest for the remainder of eternity, or at least, for my earthly life.

http://qa.jtiger.org/GetQAndA.action?qids=68&showAnswers=true
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, lofty goals, but the link doesn't seem to work for me.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, it's hosted on my home ADSL account until I can find other arrangements.
Try it again, the server hasn't gone down - it was probably busy choking my bandwidth with some other request.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks very good. I have a few quibbles, of course:

[Tony]:The Java Language Specification contradicts itself by implying that these two terms are synonymous, then in the following paragraph that a String literal is a special type of String constant. It does this by providing an example that suggests that concatenating two String literals results in a single String literal.

This "suggestion" is immediately clarified, quite clearly, in the comment on the very same two lines as the example itself. It's not an example of a string literal - it's an example of two string literals forming a string-valued constant expression.

I note that the text talks about loading from the same class definition with two different loaders, while the code example uses two different class definitions (X and Y) and two different loaders. I think it's worth noting that you could elminate class Y entirely, and get the same behavior by loading X twice. Which would demonstrate what the text is talking about.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the feedback.


This "suggestion" is immediately clarified, quite clearly, in the comment on the very same two lines as the example itself. It's not an example of a string literal - it's an example of two string literals forming a string-valued constant expression.


"Here is a horse.
<insert-cat-here/>
Actually, it's a cat."
A contradiction? Sure. But it's on the same line? So?


I note that the text talks about loading from the same class definition with two different loaders, while the code example uses two different class definitions (X and Y) and two different loaders. I think it's worth noting that you could elminate class Y entirely, and get the same behavior by loading X twice. Which would demonstrate what the text is talking about.


I agree somewhat - I added a third case.
[ April 13, 2005: Message edited by: Tony Morris ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those of you following along at home, the relevant part of the JLS we're discussing is right here.

[Tony]:

"Here is a horse.
<insert-cat-here/>
Actually, it's a cat."


Except it was more like:

"Here are examples of horses:

<insert-one-horse/>

<insert-another-horse/>

<insert-two-horses/>
Actually that's two horses."

If we omit the final comment, then it's ambiguous whether <two horses/> was intended as one example of a horse, or two examples of horses. (Well not that ambiguous since we all know what a horse is, and the difference between one and two - but the definition of a string literal is not quite as obvious to everyone.) Anyway - <insert-two-horses/> was ambiguous. Until they immediately clarified what they meant.

A contradiction? Sure.

You say contradiction, I say clarification. Particularly because...

But it's on the same line? So?

Immediacy is relevant. If I say something ambiguous and immediately follow with a statement acknowledging the ambiguity ("Actually...") and resolving it, that's a clarification, not a contradiction. If I don't notice the ambiguity, and then awhile later I say something which contradicts it (or at least, contradicts one interpretation of it) - well then you'd be a lot more justified saying I'd contradicted myself.

Also, you indicated you saw a contradiction in the next paragraph. Sounded to me like you hadn't read the rest of the lines (the comments) in the original paragraph, since by the end of those lines, the contradiction was resolved. By next paragraph, it was clear that "This is a " + "two-line string" is not one string literal, but two. Which is entirely consistent with "String literals-or, more generally, strings that are the values of constant expressions" implying that string literals were a subset of string-valued constant expressions. Also entirely consistent with the exact definitions of "String literal" and "constant expression", given in JLS 3.10.5 and JLS 15.28, respectively.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Tony]: I agree somewhat - I added a third case.

Cool - showing both techniques is even better.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic