This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt regarding strings 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 "doubt regarding strings" Watch "doubt regarding strings" New topic
Author

doubt regarding strings

NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
Hi,


In the above code two objects will be created.one string object placed in non string constant pool(s.c.p). literal "abc" placed in string constant pool.where this string constant pool present? in heap only? or it is separate memory? please any one show me diagrammatically how the single reference 'f' point to two objects(one in s.c.p,another n.s. c.p).

I know Life is very Tough...But I AM TOUGHER
SCJP 1.5
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

I dint see any reference named f.
Please post the full code for the better explanation.


My Blog SCJP 5 SCWCD 5
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

In the above code two objects will be created.one string object placed in non string constant pool(s.c.p). literal "abc" placed in string constant pool


Two objects will be created but your exlanation seems incorrect. String constant pool is just a collection of references to String objects. When you say "new String()" JVM is forced to create another object despite the String literal "abc". Check Strings, Literally for more details.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
NagarajGoud uppala wrote:Hi,


In the above code two objects will be created.one string object placed in non string constant pool(s.c.p). literal "abc" placed in string constant pool.where this string constant pool present? in heap only? or it is separate memory? please any one show me diagrammatically how the single reference 's' point to two objects(one in s.c.p,another n.s. c.p).

sorry now i corrected it from 'f' to 's'
Hi vijitha,
if you see kathy sirrae and bertbates 6 page 434 you can find out this.
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
Two objects will be created but your explanation seems incorrect. String constant pool is just a collection of references to String objects. When you say "new String()" JVM is forced to create another object despite the String literal "abc".

Hi vijitha,
your are correct.But i think two objects because one string object will be created which is referred by 's' and "abc" literal also create one object.isn't it?? if i am wrong correct me

Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
Yes, you are wrong because this statement String s=new String("abc") is going to create just ONE string object with the value: abc.

You are simply declaring a ref s to a string object then calling the constructor of the String class and passing an argument to it because constructor for String() can take arguments as literals. At the end you will end up with String ref s in the stack that is pointing to a String object in the heap that has a value stored in it with literal: abc. .

I have NO idea where the concept of creating 2 string objects came from.


Cheers!!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

NagarajGoud uppala wrote:But i think two objects because one string object will be created which is referred by 's' and "abc" literal also create one object.isn't it??


First, One object will be created from "abc" String literal (actually there is bit more to this,how this happens but that is out of SCJP scope) . And another object will be created on the "new String()" invocation which will use the reference previously created from the SCP (String Constant Pool).
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
Hi vijitha,
yes thats right.
can you show it through diagrammatically, if so it will get clear picture.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

Mo Jay wrote:I have NO idea where the concept of creating 2 string objects came from.


I suggest that you go through Strings,Literally that will give you more details on this and probably clear your doubts . And it has some nice diagrams as well
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
Thanks Vijitha for the link to: Strings,Literally

I went through the link above and unfortunately it didn't tell much I didn't know already, which makes my previous answer to the: String s=new String("abc"); question still the same.

Creating a string using the new key word has nothing to do with the String Literal Pool and string literals.

The new key word means RunTime and that's when the new string object is created with the literal abc value in it. This abc value has NO reference to it in the String Literal Pool because it was created at RunTime and NOT at class loading time.

The String Literal Pool comes into play only when you create a string using this method: String s = "abc";. In this case the string is first created in the heap with a reference to it in the String Literal Pool and this happens during the class loading time. THEN when you assign a reference to that string as in this case with s, s will be assigned the same reference (pointer) the String Literal Pool is using to point to the string object "abc" that was created in loading time. AT this point we can say that the string object created in this way has 2 references: one that is in the String Literal Pool created automatically by JVM and the other reference that we assigned to it which is s in this case, and it is located in the stack.


I hope that this is clear enough,

Cheers!!!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

Well Mo Jay, you are correct here. Previously if the program never met the same literal there's no object so far. So only one object will be created in that single statement. I was going weird in that previos replies (just ignore those about two objects).
 
GeeCON Prague 2014
 
subject: doubt regarding strings