Chaitanya Kidambi wrote:
Chaitanya Kidambi wrote:Hello everyone. i am going through Strings topic in K&B. according to K&B, whenever compiler encounters a string literal it checks the string constant pool and if it finds a match to the string it will simply assign the new reference to that string literal object rather than creating a new object.
in this case only one string object is created and two references are referring to it.
i just want to know how many string object are created in the following piece of code.
i guess only two string objects are created in the above code.
1. "abc" on line 1, referenced by s(however lost later )
2. "def" on line 2, (which is lost as it has no reference to it).
can any one please clarify my doubt.
thanks in advance
cheers
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Seetharaman Venkatasamy wrote:
Chaitanya Kidambi wrote:
Chaitanya Kidambi wrote:
how about "def" object created in line 3??
Chaitanya Kidambi wrote:Hello there,
thanks for the reply.
Seetharaman Venkatasamy wrote:
Chaitanya Kidambi wrote:
how about "def" object created in line 3?? if " def" is not stored in constant pool then it should be create again on line 3 right?
so then there will be 5 objects created and among them 4 will be abandoned.
"abc" (lost after execution of line 4)
"def" (lost, as none is referring it)-line2
"abcdef" (lost, as none is referring it) -line2
"def" (lost, as none is referring it) -line3
"abcdef" on line 4 referred by s.
cheers![]()
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Seetharaman Venkatasamy wrote:
Chaitanya Kidambi wrote:
how about "def" object created in line 3??
as I said above "def" is created in(2) and reference is placed in string constant pool. so in line 3, the new object wont created.your just passing the copy of the reference which is already in pool.
Stephan van Hulst wrote:Ikpefua, do you have a link for me where I can see that the exam doesn't care about the String pool as such? I thought they would, but you seem very convinced.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ikpefua Jacob-Obinyan wrote:
Stephan van Hulst wrote:Ikpefua, do you have a link for me where I can see that the exam doesn't care about the String pool as such? I thought they would, but you seem very convinced.
Hello Stephan, I do not have any link that indicates issues of the pool. I have written the exams twice and the questions on Strings are exactly as
described in K&B. The exam objective 3.1 says: "Discuss the differences between the String, StringBuilder, and StringBuffer classes".
What K&B focuses on in this aspect is string immutability in other to enable us understand better these differences, As far as I know there is no exam question that tests your knowledge directly about the pool, if you have any information that proves the contrary or proves that there is an objective 'directly' focused on the pool, please let us know.
Thank you for your understanding.
Stephan van Hulst wrote:No, there will be four instances created, exactly the way Seetharaman described. The String "def" on line 3 isn't created a second time because it is located in the String pool. All String literals always go to the pool.
Strings literals that don't have any local references anymore, will not be "lost". Since they still reside in the String pool, they won't be eligible for garbage collection.
Ikpefua, please Keep It Down
Since they still reside in the String pool, they won't be eligible for garbage collection.
Stephan, I hope you are not mixing this topic with garbage collection??
"lost" simply means "lost" to the current program and not garbage collection
eligibility. Thank you.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Stephan van Hulst wrote:
Sorry for hijacking your original question, Chaitanya.
Stephan van Hulst wrote:They are the same thing. If an object is lost to the program, it is automatically eligible for garbage collection.
Example: after line 3, "abc" is not lost because it is still in the String pool, and I can retrieve it using a String literal, or even through the intern() method:
I invoke this program using "java Retrieve xyz". At line C, s will refer to the exact same object it referred to in line A. So the object was never lost.
Sorry for hijacking your original question, Chaitanya.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Stephan van Hulst wrote:You are correct, if we want to continue this discussion, we should probably do it in another forum.
As for the original question, how many String objects are created, I would tend to say 4 objects, but I guess it depends on whether or not you have to take the String pool into account. Maybe someone else can comment on this.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.