The Artima Developer Community
Sponsored Link

Java Answers Forum
Where does object live?

3 replies on 1 page. Most recent reply: Mar 8, 2002 7:28 AM by Bill Venners

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
John Ren

Posts: 2
Nickname: javaren
Registered: Mar, 2002

Where does object live? Posted: Mar 7, 2002 7:32 AM
Reply to this message Reply
Advertisement
My question is that if a object has only one data member(field) of a primitive type and the object lives on the heap, where is that field placed? On the heap or the stack?
Beg another help, would you tell us the actual size of the Boolean variable? Some books tell readers it's 1 bit, otherwise, other books tell us it's 1 byte/8-bit, even there's the third issue: it's 1 int/32-bit. Which one is right? We're really confused.


Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Where does object live? Posted: Mar 7, 2002 9:42 AM
Reply to this message Reply
All objects live on the heap. Any instance variables sit within the memory allocated for the object on the heap. Any non-constant static variables sit logically within the memory set aside for the class data in the VM's "method area." Compile-time constants (static final variables assigned values that the compiler can compute) are resolved at compile time to the actual value, not a reference. So they exist wherever they are used. For more info:

JVM's Basic Architecture:

http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine2.html

The Method Area:

http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine5.html

The Heap:

http://www.artima.com/insidejvm/ed2/ch05JavaVirtualMachine6.html

Compile-Time Resolution of Constants:

http://www.artima.com/insidejvm/ed2/ch08TheLinkingModel11.html

John Ren

Posts: 2
Nickname: javaren
Registered: Mar, 2002

Re: Where does object live? Posted: Mar 7, 2002 6:41 PM
Reply to this message Reply
Firstly we thank you very much.

We are students in China. We are reading 'Thinking in Java 2nd Edition'. Possibly because of the difference of language, we meet the following question:

In section 'Where storage lives' in Chapter 2, about heap we get the information "This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. "
In section 'Special case: primitive types' in the same chapter, in the first paragraph there is an information "That is, instead of creating the variable using new, an "automatic" variable is created that is not a reference. The variable holds the value, and it's placed on the stack so it's much more efficient. "

The question is that if a object has only one data member(field) of a primitive type and the object lives on the heap, where is that field placed? According to the 2nd information, it should be placed on stack, but the object is living on the heap.

Now you've given us a deep-level and clear answer, which of course dismissed the question above.
So let us say appreciation to you again.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Where does object live? Posted: Mar 8, 2002 7:28 AM
Reply to this message Reply
You're welcome.

Flat View: This topic has 3 replies on 1 page
Topic: reading text file through applet Previous Topic   Next Topic Topic: how to protect class files from being decompiled?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use