File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String literals and garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String literals and garbage collection" Watch "String literals and garbage collection" New topic
Author

String literals and garbage collection

Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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


Tony Morris
Java Q&A (FAQ, Trivia)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Hmm, lofty goals, but the link doesn't seem to work for me.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Tony]: I agree somewhat - I added a third case.

Cool - showing both techniques is even better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literals and garbage collection