This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes About EmptyString Creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About EmptyString Creation" Watch "About EmptyString Creation" New topic
Author

About EmptyString Creation

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Folks,

When you say,

1. private String password = ""; and 2.private String password = null;

in 2nd approach nothing will create(object wont create)

my doubt is in 1st approach , whether ""(emptystring) object will cretae on Heap or it is same like private String password = null?
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
In the first approach try doing:



The result might clear up your doubt.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Gavin Tranter wrote:The result might clear up your doubt.


the answare is 0 . so you mean it will create? [ but in 2nd approach you will get null pointer exception right ? ]

still my doubt not clear friend
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
The first reference will get the address of the interned literal and the second will get the address of the reserved constant null. There is no heap allocation is this example.

To create a distinct String object on the heap use
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

any update on this ?
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

seetharaman venkatasamy wrote:any update on this ?


Ehhh what updates are you looking for? Ryan did give the answer you were looking for, right?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sam Mercs wrote:
Ehhh what updates are you looking for? Ryan did give the answer you were looking for, right?


Well, As far i know, String constant pool is nothing but collection of references to the object which is in heap.

thats why i am looking for some more update
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

I still not a 100% on what your doubt is but I'll answer your original question -

Consider you have the following statements in your program ...

I suggest you read the article pointed by Ryan - its give a pretty detailed explanation ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About EmptyString Creation
 
Similar Threads
NoInitialContextException EJB Client (Java main class)
Hibernate problem
veriables in Java Bean
File transfering from client machine to Server through FTP.
Validation Error on SelectOneMenu