File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Doubt relating to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt relating to String" Watch "Doubt relating to String" New topic
Author

Doubt relating to String

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

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: 98
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: 449

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html


http://muhammadkhojaye.blogspot.com/
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

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: 1056

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt relating to String