answersLogoWhite

0

References are stored in stack while objects are stored in heap.

Reference holds the memory address of an object, that's why they are called reference. If they don't refer to any object in heap, they hold 'null', meaning nothing.

Objects hold the actual data in them. This data is accessed (either read, or written) through reference. An object may be referenced by zero or more references.

For example, consider a class Person, which can hold 'name'.

I create 2 reference variables of Person type.

Person p1;

Person p2;

at this moment both references contain 'null'. An attempt to access 'name' data will throw an error. Now assigning new object to 'p1'.

p1 = new Person();

this causes an object to be created in heap, and its reference(memory address) is stored in p1. Now I can access 'name' data of this object created through p1.

p1.name = "Tom";

Assigning p2 to p1, will make p2 pointing the same object in the heap as p1 is pointing to. Thus if you change 'name' through p2, you are actually changing the same object as p1 is pointing to.

p2 = p1;

p2.name = "Sam";

both p1 and p2 reference variables that are in stack, are pointing to the same object that is in heap.

Hope this helps :)

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What is the difference between reference and hashcode?

There is no relation between reference and hascode, Java reference is unique pointer which refers an object. so each object will have a unique reference. but 2 diff object can have same hashcode.


What is the difference between a weak reference and a strong reference in python?

The same difference as in other languages. A weak reference is one that won't stop the garbage collector from eliminating an object.


What is The distance between an object and a reference point is the object's?

The distance between an object and a reference point is the object's displacement from the reference point. It is typically measured in a straight line from the reference point to the object.


Difference between reference and object in java?

On the lower level of Java, a "reference" can be thought of like a pointer in C. It is essentially an integer which refers to (points to) a location in memory where the object data exists. // "button" is a reference to a JButton with a "1" on it (the object). JButton button = new JButton("1");


Which term is defined as change in a object's position relative to a reference point?

The term defined as change in an object's position relative to a reference point is "displacement." It refers to the difference between an object's final position and its initial position, taking into account both distance and direction.


What is the difference between sense and reference in linguistics?

In linguistics, sense refers to the meaning or concept of a word, while reference refers to the specific object or idea that a word points to in the real world.


The distance between an object and a reference point is the object's?

The distance between an object and a reference point is the object's position in relation to that point. This distance is typically measured in a straight line between the two points and is an important consideration in physics and mathematics.


What is the distance between an object and its reference point?

Vector


What is the height of an object above a reference point?

The height of an object above a reference point is the vertical distance between the object and the reference point. It is commonly measured in units such as meters or feet. The height is determined by subtracting the elevation of the reference point from the elevation of the object.


What is the difference between a form file object and a windows form object?

difference between a form file and a form.


difference between tripitaka and jataka?

[object Object]


What is the difference between force and tension?

[object Object]