• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Where the object is created?

 
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt on where the object is created in case of string.

I have give two cases below, so can anyone tell me in each case where the object is created?




Thanks in advance.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object always stay in Heap. String Pool is collection of references which points an Object[which is in heap].

in your example there are 2 objects will create in heap.

hope this helps
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, as long as the "new" keyword is use and an object is created, it is stored on the heap. the object that variable str_2 in line 3 of your code refers to is stored on the heap
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi:


so


hope this helps!
 
Sumukh Deshpande
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Baichuan Huang wrote:hi:


so


hope this helps!



But does that mean in below case there will not be any object on pool?


String str_2 = new String("abc");


 
Baichuan Huang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumukh Deshpande wrote:

Baichuan Huang wrote:hi:


so


hope this helps!



But does that mean in below case there will not be any object on pool?


String str_2 = new String("abc");



Out of every new object is inside the heap.
when the jvm is Compileing,it will goto String pool to find the "abc"
if there is no "abc", it will create "abc" in String pool
and when the jvm is running,it will find the "abc" in String pool and create a String Object-->new String("abc") the "abc" is Copy from which Object valued "abc" in String pool
now, the new String("abc") is create at heap.
i am so sorry,at first,i forget it.
now ,hope this helps!

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Baichuan Huang wrote:when the jvm is Compileing,it will goto String pool to find the "abc"
if there is no "abc", it will create "abc" in String pool



This is wrong. No objects are created by the compiler. Objects are only created at run time.
If a class contains a String literal, that String will be created in the string pool when the class is loaded - unless it already exists in the pool in which case a reference to the existing one will be returned.
reply
    Bookmark Topic Watch Topic
  • New Topic