what happens with
q2)String literels will never garbage collected.
anyone seeing the questien,
plz spent only a very little time with it
wishing u a nice refreshment..
Both -1.0/0.0 and 1.0/0.0 produce Double.NEGATIVE_INFINITY
2)String literal are not objects so they can't be garbage collected. (However, the String objects created from the String literals may be gc)
String literal are not objects so they can't be garbage collected
It is indeed true that they are not gced, but what makes you think that they are not object??? Explain please.
You can use a String literal exactly the same way you use a normal String, you can invoke the same methods on them, so what makes you think they are not objects ???
Originally posted by Seany Iris:
Who can explain "String literels will never garbage collected. " to me?
A string literal is the characters between double quotes in a java program, like "hello world."
When you create a string in java, you can do one of two things...
String str1 = "Hello World";//set str1 to point to this string literal object already created for you when the program starts.
String str2 = new String("Hello World"); //str2 now references a brand new string object that you just created with the NEW operator.
If I now write
str1 = null;
str2 = null;
Then only the String object I created and assigned to str2 above is eligible for garbage collection. The string literal I assigned to str1 above is never eligible for garbage collection. If I now write
str2 = "Hello World";
I am re-using the same exact string literal object that I used above and originally assigned to str1. String literals are created for you automatically when your program starts, and they are never garbage collected while your program is running!
Originally posted by Rob Ross:
String literals are created for you automatically when your program starts, and they are never garbage collected while your program is running!
Is it really true that String literals are never garbage collected? Would the following code crash your machine?
This would create a rather "endless" list of String literals, correct? If they were never garbage collected, this would eventually fill up your memory and crash your JVM, correct?
However this is not a normal thing to do.
String s is not a string literal. It gets tyhe value of a string object computed after the concatenations. s will be g.c.ed