Reference Variables Tutorial

A reference variable is a variable that "refers" to an object, hence the title reference. Every variable must have a type that is declared at compile time (in your source code file). The type must be either a primitive data type or an object type.
(type) (variable Name) (;)
When the type is an object type, the variable is called a reference variable. Reference variables can only be assigned an object reference or the null literal. An object is created using the Java keyword new. Reference variables must be declared as one of the following object types: class types, interface types, and array types. Don't worry about interface types just yet, I will cover them in a future tutorial. In this tutorial I will explore some differences between a primitive data type variable and a reference variable. I will also introduce you to a special null literal that can be assigned to a reference variable.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ReferenceVariables
C:\Java>cd ReferenceVariables
C:\Java\ReferenceVariables>Notepad ReferenceVariables.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ReferenceVariables {
    public static void main(String args[]) {
        // What happens if we declare a primitive and a reference variable with no assignment?
        int a; // primitive data type 
        //System.out.println(a); // error: variable a might not have been initialized
        Box x; // object data type, class type specifically
        //System.out.println(x); // error: variable x might not have been initialized
        // Great, we now know that both types need to be assigned a value before use.
    
        // What if I want to assign a 'harmless' default initial value to the variable?
        a = 0; // for the primitive numeric types, 0 is a great 'harmless' default value
        x = null; // null is basically nothing, a great 'harmless' default value. x 'refers' to nothing.
        System.out.println("a = " + a); // a = 0
        System.out.println("x = " + x); // x = null

        // Now what if we assign our reference variable to an actual object type and display the reference variable to the console?
        x = new Box();
        System.out.println("x = " + x); // x = Box@659e0bfd, where the 659e0bfd will be a different hexadecimal number in your console.
        // very interesting ... so x refers to a Box object at what might be a memory location (more to come in later tutorials).  
        // let's try something real quick
        Box y = null;
        Box z = new Box();
        System.out.println("----------");
        System.out.println("x = " + x); // x = Box@659e0bfd
        System.out.println("y = " + y); // y = null
        System.out.println("z = " + z); // z = Box@2a139a55
        System.out.println();

        System.out.println("----------");
        // I can plainly see that we have two Box objects, Box@659e0bfd and Box@2a139a55 (your numbers will differ).
        // Now I will assign some values to the instance variables for each Box object.
        x.length = 8;
        x.height = 2;
        x.width = 6;
        z.length = 5;
        z.height = 3;
        z.width = 7;
        System.out.println("x volume = " + x.displayVolume()); // 96
        System.out.println("z volume = " + z.displayVolume()); // 105
        System.out.println();

        System.out.println("----------");
        // What will happen if I assign y to the x reference variable?
        y = x;
        System.out.println("x = " + x); // x = Box@659e0bfd
        System.out.println("y = " + y); // y = Box@659e0bfd
        System.out.println("z = " + z); // z = Box@2a139a55
        System.out.println("x volume = " + x.displayVolume()); // 96
        System.out.println("y volume = " + y.displayVolume()); // 96
        System.out.println("z volume = " + z.displayVolume()); // 105
        // So y doesn't necessarily equal x, it 'refers' to the Box object that x 'refers' to
        System.out.println();

        System.out.println("-----Assign x to z -------");
        // What will happen to y if I assign x to z?
        x = z;
        System.out.println("x = " + x); // x = Box@2a139a55
        System.out.println("y = " + y); // y = Box@659e0bfd
        System.out.println("z = " + z); // z = Box@2a139a55
        System.out.println("x volume = " + x.displayVolume()); // 105
        System.out.println("y volume = " + y.displayVolume()); // 96
        System.out.println("z volume = " + z.displayVolume()); // 105
        // y still has a reference to the object that we originally assigned to x
        // x now has a reference to the object that we originally assigned to z
        System.out.println();

        // 

    }
}

class Box {
    int length = 0;
    int height = 0;
    int width = 0;
    
    int displayVolume() {
       return (length * width * height);
    }    
}

Now switch back to the command prompt (CMD) and type in javac ReferenceVariables.java and press Enter.
Now type in java ReferenceVariables and press Enter.


C:\Java\ReferenceVariables>javac ReferenceVariables.java
C:\Java\ReferenceVariables>java ReferenceVariables
a = 0
x = null
x = Box@659e0bfd
---------
x = Box@659e0bfd
y = null
z = Box@2a139a55

---------
x volume = 96
z volume = 105

---------
x = Box@659e0bfd
y = Box@659e0bfd
z = Box@2a139a55
x volume = 96
y volume = 96
z volume = 105

-----Assign x to z -------
x = Box@2a139a55
y = Box@659e0bfd
z = Box@2a139a55
x volume = 105
y volume = 96
z volume = 105



Final thoughts

Hopefully this tutorial has cleared up a few things about the concept of an object. A class defines the template or blueprint for the members (instance variables, methods, etc.) of the object. Understanding that reference variables simply refer to an instance of an object is critical to learning more advanced concepts.


Tutorials