Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

K&B - Statement, Cannot understand

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Some Reference variables live on the Stack while some live on the heap"

This is a statement given in K&B which is true.
Can some one explain that.

I thought objects live on heap and methods,local variables and reference variables live on stack.

This was further explained by
"When and object has a reference variable ,the reference variable lives inside the object on the heap"

I didn't get either of the statements and moreover, it sounded contradictory.
Can some one please explain .
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you define a class with some instance variables:



when you instantiate a Test object the testBucket instance variable is a reference to an instance of Bucket. The testBucket reference variable lives on the heap as it is a part of the Test object.

In the makeObject() method we are defining a reference variable that will point to a new instance of Bucket - this reference variable will live on the stack as it is a local variable. All local variable live on the stack.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that no matter where the variable is defined i.e instance(on Heap) or local(on Stack) the object itself will always be on the Heap there is No Stack object.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also note that no matter where the variable is defined i.e instance(on Heap) or local(on Stack) the object itself will always be on the Heap there is No Stack object.



... JVM's are allowed in theory to allocate objects on the stack in theory in certain specific circumstances ...

"Escape analysis

The Java language does not offer any way to explicitly allocate an object on the stack, but this fact doesn't prevent JVMs from still using stack allocation where appropriate. JVMs can use a technique called escape analysis, by which they can tell that certain objects remain confined to a single thread for their entire lifetime, and that lifetime is bounded by the lifetime of a given stack frame. Such objects can be safely allocated on the stack instead of the heap. Even better, for small objects, the JVM can optimize away the allocation entirely and simply hoist the object's fields into registers. "

http://www.ibm.com/developerworks/java/library/j-jtp09275.html
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

i am not able to get these lines

when you instantiate a Test object the testBucket instance variable is a reference to an instance of Bucket. The testBucket reference variable lives on the heap as it is a part of the Test object.

In the makeObject() method we are defining a reference variable that will point to a new instance of Bucket - this reference variable will live on the stack as it is a local variable. All local variable live on the stack.

.

Can you please elaborate little(in easier way) Please..
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

According to my understanding.. testBucket reference variable will be stored on stack and actual object will store in heap.
where does i defined in line 1 will store. Stack for sure than
Isn't this the case that testBucket reference variable will store in calling method stack i.e main() or some other thread.
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic