aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String object creation 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 "String object creation" Watch "String object creation" New topic
Author

String object creation

Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

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
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Chaitanya Kidambi wrote:



*object "abc" created(count-1) in (1)
*object "def" created(count-2) in (2) also another new object called "abcdef" is created(count-3) in heap but *reference is not placed in string constant pool*
*a new object called "abcdef" is created(count-4) again at (3) in heap which is meaningfully equal to an object which is created in (2) but it is different object.

so totally there are 4 objects . but only 2 objects references["abc" and "def"] are placed in string constant pool

hope that helps
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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


Hello Chaitanya, the behaviour of the JVM / POOL And The Contents Of The Pool Are
NOT On The Exams(To The Best Of My Knowledge). K&B Simply Want You To Have An Idea
Of How This Works.
An Exam Question Will DIRECTLY Test You on String 'Immutability' And NOT Whether A String
Exists In The Pool. The Following Steps Is One Of The Best Ways To Go About It.

READ CAREFULLY:

- ONE REFEFERENCE VARIABLE (s) AND FIVE STRING OBJECTS ARE CREATED.

- IN LINE 1, REFERENCE VARIABLE (s) OF TYPE String IS DECLARED, STRING OBJECT
"abc" (LATER LOST!) IS CREATED, AND ASSIGNED TO (s).

- IN LINE 2, STRING OBJECT "def" IS CREATED AND THE concat() METHOD CREATES
STRING OBJECT "abcdef" (BOTH ARE LOST!!)

- IN LINE 3, STRING OBJECT "def" (LATER LOST), IS CREATED, THE concat() METHOD
CREATES STRING OBJECT "abcdef" AND ASSINGS IT TO REFERENCE VARIABLE (s) (AT THIS
POINT, STRING OBJECTs "def" IN LINE 3, AND "abc" IN LINE 1 ARE LOST!!).

CAN YOU SEE THAT ONE REFERENCE VARIABLE (s) AND FIVE STRING OBJECTS ARE
CREATED?? IF YES GOOD!! IF NO, SPEND SOME MORE TIME TO STUDY IT, IF POSSIBLE
PRACTICE WITH DRAWING A DIAGRAM OF THE 'HEAP' AND STRINGS BEHAVIOUR.

STUDY CAREFULLY THE EXAMPLE ON CHAPTER 6, PAGE 433 OF K&B.
REMEMBER NOT TO CONFUSE YOURSELF WITH THE POOL (FOR THE PURPOSE OF THE EXAMS!!)
I HOPE THIS HELPS.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

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

Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47



hello mate,
thanks a lot. i have a clear understanding now. so, the objects which have references will only be stored in the string constant pool. am i right ??
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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



Hello Chaitanya I Already Mentioned That "def" In Line 3 Is Lost, READ CAREFLLY... AND STOP MENTIONING POOL(FOR NOW)
The Exams Does NOT Care Whether The Objects Are In The POOL Or The Atlantic Ocean!!
CONCENTRATE On The Program And Assume That Each New String Object Is Created Right On The Program!!(Even If Something Else
Happens Behind-The-Scenes)...The Exams Does NOT Care About That!...
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

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.



hey seetharaman,

now i am confused. haha. can you please be more presice. will the string objects be stored in the constant pool when there is no reference to it.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Chaitanya, when you have a String literal in your code, the JVM will create a String object with that value on the heap. Then it will add a reference to that object to the String pool, which is nothing more than a static collection of Strings, in the String class. So when you have no more references in your own code to these String literals, the object won't be lost because they still have a static reference to them in the String pool.

Let's take a look at your code. At line 1, the String object "abc" is created on the heap, and a reference to it is added to the String pool. Then another reference to it is assigned to your s variable.
In line 2, the String object "def" is created on the heap, and a reference to it is added to the String pool. Another reference to it is passed to the concat() method.
The concat method will create a new String object, "abcdef", which is printed, and then subsequently lost, because there are no more references to it. (Remember that only String literals get added to the pool).
In line 3, the JVM sees that "def" has already been added to the String pool, so it won't create a new String object. It just reuses the reference in the pool, and passes it to the concat() method.
The concat method will then once again create a new String object "abcdef", and a reference to that object will be assigned to s. Note that this will replace the reference to "abc", but "abc" is not lost because it still has a reference in the pool.

So "abc" and "def" are created once, and "abcdef" is created twice.
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

thanks stephan, i think i got it now. and thanks everyone for replying.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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.
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

Ikpefua Jacob-Obinyan wrote:
Chaitanya Kidambi wrote:Hello there,
thanks for the reply.


Seetharaman Venkatasamy wrote:
Chaitanya Kidambi wrote:







hey man
check out here

thanks
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.

No, I'm not sure myself. I guess what is the case is that they won't ask direct questions about the String pool, but I think you're still supposed to know that for two identical String literals, only one object will be created.

Anyway, I have never done the exam, so don't take my word for it.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

Stephan van Hulst wrote:
Sorry for hijacking your original question, Chaitanya.


hahaha, its ok pal. and yeah you are right about the inTern() method. i have gone through couple of articles on java ranch. thanks a lot for your help buddies and all the very best if you are appearing for the exam .

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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.


Hello Stephan,
An Object is eligible for garbage collection when no reference variable refers to it, and you can call it 'LOST' or 'WHATEVER' you feel confortable with.
Can you prove to us that the intern() method is on the exams??
Chapter 6 page 431 of K&B book uses the expression 'LOST' at least twice, 'LOST' simply means; in a program when you create a String and assign it to a reference variable, if you reassing that same reference variable to another String, the inicial String it referred to will be 'LOST' to the program.



The output is: lost? and be rest assured that this is what you are MOST likely to be tested for in the exams.
I PERSONALLY will follow that theory, as long as we are all in this particular forum to prepare for the exams and as long as K & B says we should consider it that way. IT IS TRUE that the String pool exists, but that is not FUNDAMENTAL towards preparations for the exams, however it is important to know of its existence and basic characteristics.
The concept I advise that all of should understand (in our preparations for the exams) is String immutability.

Understanding the concept of String immutability will help us answer Objective 3.1 of the exams, which will simply test your knowledge on the differences
between String, StringBuffer AND StringBuilder.
After passing the exams, as a programmer in the real world, futher details about the String pool in cordination with the JVM might become necessary.
I hope you understand my point.

I do NOT agree with mixing up a garbage collection theory with this topic on Strings. Chapter 3 page 256 of the K & B book says quote "Due to the vagaries of the String constant pool, the exam focuses its Garbage collection questions on Non-String objects". And since we are in this particular forum to prepare for the exams, dont you think we should focus EXCLUSIVELY on the interests of the exams??
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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.


Thanks Stephan, everything I have explained above simply indicates that in another forum I will not argue with you, because you are PERFECTLY correct
on your description about the String pool, and I knew that before, thanks to my carefull study of the basic characteristics/functions of JVM/String pool.
The positive thing here is that this arguements helps us to get futher prepared for the exams.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String object creation