• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

doubt regarding strings

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dint see any reference named f.
Please post the full code for the better explanation.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vijitha,
yes thats right.
can you show it through diagrammatically, if so it will get clear picture.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic