3. Primitive and Reference variables in Java

Thilini Fernando
4 min readJun 24, 2022

--

Java variables are divided into two main categories which are primitives and object references. Primitive variables hold fundamental values including integers, Booleans, and floating points. Object References hold references to objects. All the variables must declare with variable type and name.

Variable Name

A variable need a name and the name must follow the below rules.

  • Can’t start with a number.
  • Start character must be a letter or “$” sign or “_”.
  • After the first letter, you can add numbers to the name.
  • An important one is a variable name is not a Java keyword (Java’s reserved words).

Primitive Variables

As per the above chart, Primitive data types have their own size and specific value and data type also matter. For example,

int x = 16;
byte b = x ;

The above code doesn’t work after 16 in the byte range. This is because variable types are like containers and here byte type container size is less than int container that’s why It doesn’t work.

And see the below image for a better understanding.

But, we can cast bigger data type variables into small size data type variables through casting.

int x = 16;
byte b = (byte) x;

Object Reference Variable

We think that the object reference variable holds an object. But, the object reference variable holds bits that represent a way to access an object. Object reference variable like remote control of an object and It has a type but it doesn’t have exactly a size. The below image from the HeadFirst Java is the best way to understand it.

Let’s see the 3 steps of object declaration, creation, and assignment.

1 → Declaration —Tell the JVM to allocate space for the reference variable whose name is the dog and Whose type is Dog forever.

2 → Creation — Tell the JVM to allocate space for a new Dog object on the heap.

3 → Assignment — Assign a new Dog object to the dog reference variable.

Important

  • You cannot do arithmetic operations on reference variables in Java.
Dog dog1 = new Dog();
Dog dog2 = new Dog();

In the above code example, dog1 and dog2 both reference variables hold remote control of two new Dog objects. Then, I add the below code line.

Dog dog3 = dog2;

When this line executes, The JVM allocates space for the dog3 reference variable, and all the bits inside dog2 are copied into the dog3 variable. dog2 and dog3 both hold two different copies of the same value. Then, I add the below code line.

dog1 = dog2;

When this line executes, the bits inside the dog2 reference variable are copied and stuffed into the dog1 variable. Now, dog1 and dog2 refer to the same object same time one Dog object is eligible for the Garbage Collection.

Likewise, Java Arrays are objects too. But, values of the array (each element) can be a reference variable or primitive.

I hope you understand the primitives and references in Java.

Happy Learning!

References

Sierra, K. and Bates, B., 2008. Head First Java. Sebastopol: O’Reilly Media, Inc.

--

--