Difference Between Reference and Object in Java (With Table)

Reference vs Object in Java

Objects and References are both terms associated with a ‘class’ in the java programming language.

A class is a blueprint or a template that provides instructions on how to create objects. The object bases itself off on this structure, and the necessary fields according to that class template are filled.

A memory reference, is simply as the name states, a reference to the memory slot. The user can use the memory reference to find out the address of where the object is stored.

The difference between an object and a reference is that, an object is an instance of a class, and is stored in a certain memory slot. A ‘reference’ points to the place where the ‘objects’ variables and methods are stored. 


 

Comparison Table Between Reference and Object in Java (in Tabular Form)

Parameter of ComparisonObjectReference
Basic definitionIt is the instance of a class and all the elements it contains are based on the blueprint of the class. A simply memory reference that points to where the object is stored in memory slot.
Format for creationThere is a simple format for creation of an object:
ClassName reference_variable = new ClassName(with parameter);
The memory reference is created alongside object. It is used in the format for object creation and is given a variable name
ElementsIt contains methods and variables based on the class.It contains a sequence of bits that store the address of the object.
MutableObjects have states and behaviors that can be changed, i.e, the state of the object can be changed.The reference variable value cannot be changed. It can only remain as the data type that it was declared as.
Virtual meaningIt is a real world entity that holds some form of memory or data.It is nothing but a variable name, which has no real meaning. It is like the name of a person, that references that person.

 

What is Reference in Java?

A reference is something that provides the address to the place where the object memory has been allocated.

The object is never directly used; we instead assign reference variables, which indirectly act as a middle man, between the object being called and the object stored in the memory.

Java also allows for 4 different types of references –

  1. Strong Reference.
  2. Weak Reference.
  3. Soft Reference.
  4. Phantom Reference

A reference is created within the format for the creation of an object from a class –

ClassName  reference_variable = new ClassName(with parameter);

Here the ‘reference_variable’ is the value assigned to the reference. This value cannot be changed, and it is of one data type only. However, an important thing to note about references is that the same references can be assigned to multiple objects if needed.

Reference in java
 

What is Object in Java?

An object is simply an instance of the class. It represents the structure of the class and follows the blueprint instructions for all the elements present within it, such as methods and variables.

Objects are seen to have states and behaviors. Here, states are an ‘abstract’ entity, which contain all the properties of the objects, as well as the values of those properties. Simply put, a state for a human being would be – name, race, age etc.

RECOMMENDED
Difference Between JPEG and PDF (With Table)

Behaviors binds the structure of the object. They bind the relationships between the attributes, so as to allow the object to perform functions/operations when it is called. In simple terms, we could say that for a human being, the behaviors would be- cooking food, walking, talking etc.

The ‘Object’ is created using 3 distinguishable steps-

  1. Declaration
  2. Instantiation
  3. Initialization

In the first step, declaration, a variable name of object type is declared, which goes onto become the reference for the object memory.

In the second step, instantiation, we have a keyword, namely ‘new’, which is used to create the object.

In the third step, initialization, we have the ‘new’ keyword which is followed by a ‘constructor’, which has the same name as the class from which the object is being created, along with parameters. This calls the constructor to help us initialize the new object that we are creating.

The compiled format for creating the object is as such –

ClassName   reference_variable =  new  ClassName(with parameter) ;

Here, the reference is created using ‘reference_variable’, and ClassName(with parameter) is to call the constructor.

Example – Person stu1 = new Person();

Here, Person refers to the class, stu1 is the value of the reference variable that points to the new object being created, and Person() is the constructor.

Object in Java

Main Differences Between Reference and Object in Java

  • An object is an instance derived from the structure of a class. A reference is just a variable that points to the location of the object in memory.
  • An object is created with a certain format – “ClassName reference_variable = new ClassName(with parameter);”. A reference is created alongside the object creation, within the format.
  • An object contains elements such as variables and methods, as well as states and behaviors. A reference simply contains a sequence of bits.
  • An object is never directly seen within the program, instead, the reference variable is assigned, and a reference to the object is created.
  • An object is a real – world entity that holds some memory. A reference has no real meaning and its behavior is the same as that of a name of a person.

 

Conclusion

Objects and references are both intertwined when it comes to their functioning. An object cannot be directly seen or assigned, so instead we use reference to allocate a name or a variable that acts as a middle man.

Reference allows us to indirectly call the object, as it knows the address of where the object is stored. Whilst creating an object, we can see that the reference is created right alongside it, and as it is a basic and simple format, so it saves time and is efficient.


 

Word Cloud for Difference Between Reference and Object in Java

The following is a collection of the most used terms in this article on Reference and Object in Java. This should help in recalling related terms as used in this article at a later stage for you.

Difference Between Reference and Object in Java

Table of Contents