*
The moose likes Beginning Java and the fly likes difference between object and reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between object and reference" Watch "difference between object and reference" New topic
Author

difference between object and reference

Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
When we write,

ClassA obj = new ClassA();

what is the reference and what is the object. I am reading about garbage collection and I am really getting confused by therse two terms. When we say that an object is garbage collected, do we mean the memory allocated by new to obj is being garbage collected.

Is there any text that explains about the heap, stack frame pictorally or using an java code example. I am not able to make a picture of whats happening in the background.

regards,
vijay.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The "new" operator creates a new object of the ClassA type on the heap and returns a reference to the object. The reference is then assigned to the obj variable. As obj is a local variable, it lives on the stack.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Perhaps TIJ gives the best explanation for this.
[ January 03, 2006: Message edited by: Stuart Ash ]

ASCII silly question, Get a silly ANSI.
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
and this 'obj' variable will be popped out of teh stack when the control comes out of the function in which it was declared.

What happens to the memory space in the heap. Is this where garbage collection comes into the picture. It collects the garbage from the heap.

regards,
vijay
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
It may help to think "garbage collectable heap" (and not just "heap").

When there is no reference to this memory on the gc-heap, then the gc removes the memory the next time it decides to clean up.

Stuart
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
The 'new' operator will create an object in HEAP an not in STACK.

The memory address of the heap location where the object is strored is the REFERENCE.

All the references are stored on the stack.

The GC will make a reference count for cleanup. If the count becomes 1 the object will be GC'd. The 1 reference will be that of the garbage collector.

Hope I am not making things more complex.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Pradeep KG:
The 'new' operator will create an object in HEAP an not in STACK.


Did someone suggest otherwise?


All the references are stored on the stack.


Nope. If the reference is stored in an instance variable (aka field), it's stored on the heap, obviously. Only local variables (including parameters) are stored on the stack.


The GC will make a reference count for cleanup. If the count becomes 1 the object will be GC'd. The 1 reference will be that of the garbage collector.


No, java garbage collectors don't use reference counts, but more sophisticated algorithms. With only reference counting, an object that is only referenced by unreachable objects wouldn't get garbage collected, leading to memory leaks due to reference circles. Just imagine an object referencing itself.
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
In other languages, a Java "reference" might be called a pointer or address. A Java variable simply holds the value of the reference/pointer/address, which the Java Virtual Machine uses to find the actual object in memory.

Hope that clarifies rather than obfuscates!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Adam Richards:
In other languages, a Java "reference" might be called a pointer or address. A Java variable simply holds the value of the reference/pointer/address, which the Java Virtual Machine uses to find the actual object in memory.

Hope that clarifies rather than obfuscates!


When the variable is not a primitive you mean.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Ilja Preuss:
The "new" operator creates a new object of the ClassA type on the heap and returns a reference to the object. The reference is then assigned to the obj variable. As obj is a local variable, it lives on the stack.

Does that help?


To add, the starting point to make the distinction is the lifetime-predictability of the variable (reference).

Since, for objects, the lifetime is unpredictable, they are always in the heap.

For in-method variables (primitives and references), like Ilja explains, the lifetime is predictable, and hence in the stack.

For instance variables (primitives and references), the lifetime is unpredictable, hence the heap.

This holds for both references and primitives.

I hope this properly summarizes the concept.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Stuart Ash:


To add, the starting point to make the distinction is the lifetime-predictability of the variable (reference).

Since, for objects, the lifetime is unpredictable, they are always in the heap.

For in-method variables (primitives and references), like Ilja explains, the lifetime is predictable, and hence in the stack.

For instance variables (primitives and references), the lifetime is unpredictable, hence the heap.

This holds for both references and primitives.

I hope this properly summarizes the concept.


Any objections to this? The above sums it up fine? Ilja?
[ January 05, 2006: Message edited by: Stuart Ash ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Sounds good to me.

The reason that the lifetime place a vital role in deciding between heap and stack is that stack space gets allocated when entering a method, and deallocated when the method is left. That is, we need to exactly know how much space is needed while the method is executed, and the space is only available during the lifetime of the method.
badri nath
Ranch Hand

Joined: Dec 21, 2005
Posts: 57
Hi Stuart
I was very confused about this concept and even was going through other threads in which this is discussed.
But your concept of lifetime makes it clear for what is stored on heap and what is stored on stack.Thanks very much .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between object and reference
 
Similar Threads
garbage collector
Singleton Class and Garbage collection
Scope and classes/methods
Will this cause a memory leak?
need help