• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where the object is created?  RSS feed

 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
 
Adeiza Yusuf
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
 
Baichuan Huang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi:


so


hope this helps!
 
Sumukh Deshpande
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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!

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!