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 A question on GC and String literals. 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 "A question on GC and String literals." Watch "A question on GC and String literals." New topic
Author

A question on GC and String literals.

Amol Keskar
Greenhorn

Joined: May 18, 2001
Posts: 23
Hi,
here is the question
Question ID :965441698130
How many objects are eligible for garbage collection when the control reaches line 4 if the method process() is called with the argument of 5?

1. public void process(int count)
{
2. for ( int i = 1; i < count; i++ )
{
3. Object temp = " Hello "+i;
}
4.
}
Correct Answer = 0
I know that string literals are not GCed since they are in the "pool". But I thought creating a string like below,
int x = 1;
String str = "Hello"+x; always returns a "new" string which is not in the pool (since this is determined during runtime). I thought this was equivalent of saying
String str = new String("Hello1");
The explanation to their answer is
"All the String objects are interned ie. Java caches all the string objects (except which are created by doing new)"
I thought when you create a new String object (using "new") or like in the case above (String str = "Hello"+x) the string is never interned to the pool unless we explicitly call
str.intern().

In order to convince myself, I wrote the following code,
/* Code */
class Strings
{
public static void main(String[] r)
{
String s = "hello1";
String p = "hello1";
System.out.println(s);
System.out.println(p);
System.out.println(s==p); //prints true
for(int i = 1; i < 2; i++)
{
s = "hello"+i;
}

System.out.println(s);
System.out.println(s==p); //prints false
}
}
/* End */
While on the same topic, Can someone explain exactly what the method intern() does to a String?
Can someone explain to me their answer?

Thanks!
Amol
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
Hi Amol,
you said:I know that string literals are not GCed since they are in the "pool". But I thought creating a string like below,
int x = 1;
String str = "Hello"+x;
always returns a "new" string which is not in the pool (since this is determined during runtime). I thought this was equivalent of saying
String str = new String("Hello1");
----------------------------------------------------------------
I agree with you.I also feel that the answer given for that question is wrong as new Strings are formed each time, as long as the condition is met.
Regarding your sample code,if you try the same with the addition of one more line
for(int i = 1; i < 2; i++)
{
s = "hello"+i;
s=s.intern(); //line added
}
you'll see that (s==p)returns true!
The String class has an intern method, which is used to set up pools of strings. If I have a string s and I say:

s = s.intern();
then the contents of s are compared against an internal pool of unique strings, and added if this particular string's contents are not already in the pool. A reference to the unique pool String is returned.
Strings that have been interned can be compared to each other using the == operator, because there's a unique String object for any given sequence of characters representing one of the strings. Interning offers a performance advantage in the situation where the same strings are used repeatedly.
Rashmi.
Amol Keskar
Greenhorn

Joined: May 18, 2001
Posts: 23
Thanks Rashmi!
While on a similar topic,
The below lines of code compile and print out the desired output with JDK 1.3
But JQ+ says that the lines wont compile. I am not sure if they dont compile on JDK 1.2 But if that is the case what should be the answer?
I know Sun's exam is based on JDK 1.2 but I hope Sun does not include questions that have different outputs on different versions of JVM.
System.out.println(null+true); // prints nulltrue
System.out.println(null+null); //prints nullnull
Thanks,
Amol
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question on GC and String literals.