Adam Gibson
Adam Gibson
Published in
3 min readApr 11, 2018

--

In Java, there are two kinds of data types: primitive types and references types. There are eight primitive types: int, double, char, byte, Boolean, short, long, and float. Each has a specific size and set of possible literals. Reference types are everything else. They refer to objects.

The value of a primitive type variable is the value of the primitive type itself. The value of a reference type variable is the address of the object in the computer’s memory.

Creating a new object and assigning it to a variable allocates space for the object in memory and assigns the address of the object’s location as the variable’s value.

Scanner input = new Scanner(System.in);

In this example, an instance of the Scanner class is created, and space is allocated for it in memory. The value of the input variable is the address of the object’s memory location.

When variables are passed as arguments to methods, copies of their values are passed to the method’s parameters. This is because everything is passed-by-value in Java. For primitive types, this means the value of the variable is copied.

public class PrimitiveTypes {
public static void main(String[] args) {
int i = 5;
System.out.println(i);
doubleInt(i);
System.out.println(i);
}
public static void doubleInt(int anyInt) {
anyInt *= 2;
System.out.println(anyInt);
}
}

The output of this example is:

5
10
5

The reason the value of i is still 5 is that the value of 5 was assigned to the method parameter anyInt. Then the value of anyInt was doubled, however once the method returned, anyInt was no more and i still held the value of 5.

For reference types, the value of the reference (the memory address) is copied and passed. Objects themselves cannot be passed to methods. If you pass a reference type argument to a method and change where it points to, the original copy will still point to the same place.

public class ReferenceTypes {
public static void main(String[] args) {
String s1 = "Cat";
String s2 = "Dog";
System.out.println(s1);
System.out.println(s2);

swapReference(s1, s2);
System.out.println(s1);
System.out.println(s2);
}
public static void swapReference(String str1, String str2)
String temp = str1;
str1 = str2;
str2 = temp;
System.out.println(str1);
System.out.println(str2);
}
}

The output of this example is:

Cat
Dog
Dog
Cat
Cat
Dog

As you can see, s1 and s2 still point to the same objects even though the method variables have been swapped and do point to different objects. This is because when s1 and s2 were passed to the method, copies of their values were passed to str1 and str2. So only those copies had their values swapped. The values of s1 and s2 were unaffected. Another explanation of this can be found here.

However, in Java, objects can be manipulated by reference. This means that though users cannot change where the original reference points to, the copy of the reference can manipulate the object just like the original reference. This is because the original reference and the copy both point to the same object.

public class ReferenceTypes {
public static void main(String[] args) {
int[] array = {1,2,3,4};
doubleArray(array);
for(int i : array) {
System.out.println(i);
}
}
public static void doubleArray(int[] anyIntArray) {
for(int i = 0; i < anyIntArray.length; i++) {
anyIntArray[i] *= 2;
}
}
}

The output for this example is:

2
4
6
8

The array was modified by the doubleArray() method. This was possible because anyIntArray was given a copy of the array’s address, and since in Java, objects can be manipulated by reference, then any reference that points to an object can manipulated just as much as the original.

In summary, the value of primitive type variables is the value of that primitive and the value of reference type variables is the address of the object’s location in memory. When these variables are passed to methods, copies of their values are given to the method parameters. This means that the values of the original variables are never changed by methods. However, since objects can be manipulated by reference, then methods can alter objects with the copy of the address they are given.

--

--