Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt relating to String

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s=new String("hello")


how does it create two objects? and does hello goes in string pool
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,

hello goes to string pool and one more obect will be created on heap.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
many time this topic is discussed here . do *search*
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,

hello goes to string pool and one more obect will be created on heap


Hi Brij/Vishal,
I dont think the object will be created in the pool... Rather, only a reference will be placed in the literal pool.
So, ideally only one object is created and that is in the Heap.

Correct me if I am wrong!

Regards,
Sriram
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


String s=new String("hello")

by reference you mean s variable?

hello string literal will be placed in the string pool which is an object.

Let me know if I am not correct.
 
Vedha Vishali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes Brij Garg is Correct.

Thanks,
VedhaVishali.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But hello is an Object ,so it should be created in Heap itself,then why does in go into the string pool,

what is string pool actually,is it same as memory?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Vishal Hegde : did you go through the link what Muhammad has given?
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i did ,in SCJP 6 its given that string literals go to pool and in that link its given that reference go to the pool which one is true
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic