Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About EmptyString Creation

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first approach try doing:



The result might clear up your doubt.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any update on this ?
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:any update on this ?


Ehhh what updates are you looking for? Ryan did give the answer you were looking for, right?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic