answersLogoWhite

0


Best Answer

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

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is difference between reference and object?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


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");


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

That can't be told. You would have to measure that or figure it out yourself. There is no way anyone can give you an actual answer.


What do you know about an object if it changes position relative to a reference point?

your question is a but vague. the answer refers to the Newtonian aspect of it: if you have a reference point, then you can measure the difference between the two places of that object. taking time into account, you can derive its velocity and acceleration rate. matar


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

Vector


What is the difference between an object's speed and a object's velocity?

The difference between an object's speed and an object's velocity is that the object's speed is how fast it is going, and the object's velocity is how many units of speed the object has traveled.


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]


What is the difference between nature miracles and healing miracles?

What is the difference between nature miracle and healing miracle


What is the difference between Estate Management and Business Management?

What are the difference between estate management and business management