Originally posted by ms:
Hi all ,
Can somebody tell me the correct answer and explain why is that so ?
Which is the earliest line in the following code after which the object created on the line marked (0) will be a candidate for being garbage collected, assuming no compiler optimizations are done?
public class Q76a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b;
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
}
}
Originally posted by Tanveer Mehmood:
Hi Lam,
What about the Line 1 where C is created using b. Will it create a new string or otherwise. I think C should also be pointing "bye". Correct me if wrong.
Tanveer
Originally posted by nachiket deshpande:
Hi Lam
I may be wrong but please explain to me,
when we say
String d=b;
does'nt it mean that reference of b object is assigned to d,and so now the b object does'nt have a refence.
also when b=a;//2
can we say reference 'a' is now reference 'b'?
please help..
Originally posted by ratul banji:
Hi Lam,
I fully agree with u and ur explanation.
But..why u r calling it line 4 ....is it line 4 or line 3.
!!!
Originally posted by Cameron Park:
What about String literal pool? When you create a String object like new String("line"); It is an object that can be garbage collected. But when you say b="line"; "line" is kept in the literal pool and will not be garbage collected.
Originally posted by nachiket deshpande:
Hi Lam!
again need your advice..
String b="bye";//0
String d=b;//1
b=a;//2
d=a;//3
what i get from the above code is at line 1 when reference b is
assigned to d,object "bye" has no reference.and also as you said at line 3 and 4 it's just referene copying.so does'nt it say that after line 1 executes "bye" is eligible for G.C.
please help.
Originally posted by ms:
Thanks a lot Lam for a detailed note .
Want to just check on one issue :
Do u mean to say that when we make a literal in String pool ....a new String instance is created at the heap .Then why does the result for two string literals say s1 and s2 comes out to be true
Example :
String s1 = "Hello" ;
String s2 = "Hello" ;
s1 == s2 ... gives true
Their behaviour should also be like String objects ?
2nd Point :
As per you :
WHen I declare String literal :
String b = "bye" ;
String c = b + "!" ;
It will be resolved as (object pointed by refrence b) + "!"
for once which will evaluate as bye! and assigned to refrence c
at compile time .After this b has no relevance in the c's context .
AmI right in my understanding !
Hope am not that ambiguous !
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.
The Java programming language requires that identical string literals (that is, literals that contain the same sequence of characters) must refer to the same instance of class String. In addition, if the method String.intern is called on any string, the result is a reference to the same class instance that would be returned if that string appeared as a literal. Thus,
must have the value true.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Hi Shailesh ,
I would appreciate if u also help out in resolving the queries apart from doing this good work of alerting people .
Consider Paul's rocket mass heater. |