• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Object Reference tutorial created by Ankit

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,
In your tutorial at http://www.jforeach.com/difference-between-object-and-reference-in-java/62 at second diagram, you represented name="Eric" string within Heap which I feel may not correct. What I am trying to say is when we say, String name="Eric", Eric should be created in string constant/memory pool and when we say String name = new String("Eric"), Eric should be created on Heap know ? Please advice if I am wrong.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String name="Eric", Eric should be created in string constant/memory pool and when we say String name = new String("Eric"), Eric should be created on Heap



Both will be allocated in heap...

In first case, when other reference variable refers the same string literal "Eric"... Compiler will make reference variable to refer the existing Eric string literal in literal pool...

In second case, due to "new" keyword, each time new instance is created and so "Eric" literal will be created more if this kind of statements are used more in the code and lead to Heap Overflow...
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You said, Both [ String name="Eric" and String name = new String("Eric") ] will be allocated in heap... Means, JVM will create Sting literal pool in some portion of Heap area ?
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String Literal is always stored in the String pool.

In the code below:

The variable name directly refers to the String literal "Eric" in the string pool.

But, in the code below:

The variable name refers to new String Object in nonpool memory, which contains String literal "Eric" in the String pool.

You can read more in this thread
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, Ram mentioned "Both will be allocated in heap" in his reply..That's why I am confused..Back to my original post, the second diagram appears incorrect to me..Am I confusing you guys?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String literal pool is also a part of the Heap...
reply
    Bookmark Topic Watch Topic
  • New Topic