Primitive and Reference Types in Java with Examples

In this article, I will share what is primitive and reference types in Java with examples. We have two different categories in Java these are primitive types and reference types

Primitive types are byte, char, int, long, double, short, and boolean. The classes which specify the types of objects are the reference types for example Integer, Double, Long, etc.   

When it comes to initialization of primitive types, they are initialized by default for example boolean is initialized to false and the others are initialized to 0. They also can store one value of their declared type.

Reference type variables are objects so they store the location of an object in the memory and they initialized by default the null value.  

Primitive Types in Memory

The primitive variables contain the value. 

In the below memory view, the “variable i” value is 23 and its memory address is 4567 whereas the “variable c” value is ‘d’ and its memory address is 5234.

Reference Types in Memory

In reference type, variables contain the address of the value.

reference and primitive types in java

In the below memory view, the variable str value is pointing the value and its address is 2675. When we look at the address 2675 the value is “sw test academy”. In this way, we can say that the variable str is pointing to the value “sw test academy”.

In the below example, we will see both primitive and reference types in the memory together to understand the concept better.

Based on the above code, num1 and num2 as you see below memory illustrations have different addresses and hold the value of 22 but the String variables are pointing to the same value and their values are the address of the value “sw test academy” which is 900.

Let’s do some examples and see how they behave in the code.

Primitive Type in Java Example

Primitive types are behaving pass by value in Java and we will see this in the below example. The value of the variable a is not changing after we are modifying it in the modify method because we are sending the copy of the variable a, not its reference. In this way, in the modify() method, we are changing the “value of the copy a” not the “real a” or “reference of a”. In programming, we are calling this behavior “pass by value”.

public class PrimitiveTypeExample {
    @Test
    public void primitiveTypeExample() {
        int a = 8;
        System.out.println("Before Modify: " + a);
        modify(a);
        System.out.println("After Modify: " + a);
    }

    private static void modify(int a) {
       --a; //Here, the value of a will not change because a comes here not as reference.
        System.out.println("Inside the method the value of a: " + a);
    }
}

Output

primitive type in java

Reference Type in Java Example

In the below example, the modifyAndSum() method does not change the original array because, inside this method, we are creating the copy of the array and doing operations on this copied array. That’s why before and after the modifyAndSum() method inside our test the sum of the array elements are the same. However, we send the reference of the array to the sum() method that’s why in the sum() method we are changing the original array. This is called “pass by reference” in programming.

public class ReferenceTypeExample {
    @Test
    public void referenceTypeExample() {
        int[] myArr = new int[] { 4, 5 };

        System.out.println("Before modifyAndSum method:" + (myArr[0] + myArr[1])); //The sum of the numbers in the array before everything.
        modifyAndSum(myArr);
        System.out.println("After modifyAndSum method:" + (myArr[0] + myArr[1])); //modifyAndSum method did not change original array.
        sum(myArr);
        System.out.println("After sum method: " + (myArr[0] + myArr[1])); //Sum method changed myArr because of reference type!
    }

    private static void modifyAndSum(int[] arr) {
        arr = Arrays.copyOf(arr, arr.length); //Here, we are creating a new array! The modifications won't affect original array.
        arr[0]--;
        System.out.println("Inside modifyAndSum method: " + (arr[0] + arr[1]));
    }

    private static void sum(int[] arr) {
        System.out.println("Inside sum method: " + (arr[0] + arr[1])); //Here we are adding values of original array.
        arr[0]--; //Here, we are modifying the original array because we sent as reference.
    }
}

Output

reference type in java

GitHub Project

https://github.com/swtestacademy/java-functional/tree/main/src/test/java/basics/datatypes

Thanks for reading.
Onur Baskirt

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.