| Author |
Doubt relating to String
|
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 972
|
|
String s=new String("hello")
how does it create two objects? and does hello goes in string pool
|
http://www.lifesbizzare.blogspot.com || OCJP:81%
|
 |
Brij Garg
Ranch Hand
Joined: Apr 29, 2008
Posts: 234
|
|
Yes,
hello goes to string pool and one more obect will be created on heap.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
many time this topic is discussed here . do *search*
|
 |
Sriram Sharma
Ranch Hand
Joined: Apr 12, 2006
Posts: 89
|
|
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
Joined: Apr 29, 2008
Posts: 234
|
|
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
Joined: Mar 24, 2010
Posts: 6
|
|
Hi,
Yes Brij Garg is Correct.
Thanks,
VedhaVishali.
|
 |
Muhammad Khojaye
Ranch Hand
Joined: Apr 12, 2009
Posts: 341
|
|
|
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
|
http://muhammadkhojaye.blogspot.com/
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 972
|
|
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
Joined: Jan 28, 2008
Posts: 5575
|
|
|
@Vishal Hegde : did you go through the link what Muhammad has given?
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 972
|
|
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
|
 |
 |
|
|
subject: Doubt relating to String
|
|
|