[OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Originally posted by Larry D. LeFever:
local String-literals cause heap-allocation.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
From the constant pool; that's what the ldc instruction does.Originally posted by Larry D. LeFever:
I think, now, the issue is: whence cometh that "someString"? That is, from where is it loaded (as by that "ldc" instruction)? From the constant pool or from the heap?
The interviewer was simply wrong.My thought was that it's loaded from the constant pool, so there's no added burden placed upon the garbage-collector [...] The contention, by the interviewer, was, to the contrary, that the use of such a String-literal causes heap-allocation and therefore adds burden to the gc.
If you want to be absolutely precise, the JVM may instantiate (if necessary) and intern() the String literals at class load time, or lazily when the ldc first executes. The JVM spec does not seem to favour either approach but it seems that lazy loading is the norm in JVMs. However, this is a VM implementation choice that is totally irrelevant to the question at hand. There is no difference whatsoever between the two in terms of garbage collector load.Originally posted by steveq9t4:
[...] Just did a bit more reading - it seems that "someString" MAY allocate a new String object depending on whether it has been seen before. If it has not been seen then a new String object will be created on the first pass, if it has been seen then it depends on whether it has been eligible for garbage collection since that time. [...]
42
Jeroen, you're missing the point here. The issue is not whether they exist on the heap - of course they do, whether they be inline (local) literals or string constants: all objects live on the heap. The issue is whether there is any important allocation difference between the inline literals or string constants, and specifically whether one or the other is preferable with regards to garbage collection behaviour under high loads.Originally posted by Jeroen Wenting:
Your interviewer would be right even if the JLS said nothing about String objects existing on the heap.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Steve Quintana:
Sorry about the name folks, I have now updated it.
One other thing we also likely agree on is that it is not a very good question to be asking in an interview
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Jean-Claude Dufourd:
On the relevance of the question in the context of an interview, it looks like this interviewer read the Guerilla Guide to Interviewing, by Joel Spolsky (http://www.joelonsoftware.com/articles/fog0000000073.html).
This question really feels like "3. Impossible Question"
No ?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Incidentally, a book that I was tech editor for. The plot thickensOriginally posted by Larry D. LeFever:
[...] Interestingly, that site points back to JavaRanch! [...] Margarita Isayeva (this is my real name![]()
co-author of "Mike Meyers' certification Passport Java 2"
Yes. That's what I meant by lazy loading - instantiating and intern()ing them all at class loading time I would describe as eager loading. Apologies for not keeping my terminology unambiguous.Originally posted by Steve Quintana:
I would be suprised if lazy loading was used since it is imposing a hit that may be unnecessary if the code that uses the literal is not actually executed. It seems to make more sense that instantiation would be done on an as-needed basis to minimize the class load time.
Squanch that. And squanch this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
|