1.1. Introduction

The goal of this chapter is to provide readers with a concise review of important object-oriented programming language concepts using familiar terminology. Some terms may already be familiar to some readers, but it’s essential to take special care to understand the differences between terms that appear to be similar.

For example, variables, which are discussed in detail in the next section, are a very important part of most Java programs, and most readers with some background in programming can set up a variable and use it in their code. That being said, consider the terms “primitive type variable” and “reference type variable.” Each term describes a kind of variable, but the terms are not interchangeable.

When used to describe a Java variable, “primitive type” and “reference type” each introduce additional context that limits the set of potential values that a variable can store, determines the language syntax that is supported when using the variable value, and provides insight into how the variable’s value and related information will be stored in the computer’s memory at runtime. These additional nuances are so important that a programmer would not want to confuse one kind of variable for the other, because doing so will likely prevent their code from compiling or worse, lead to runtime errors.

Note

For the question below, write your answers in your notes before looking at the solutions! Do the best you can and don’t worry if you don’t know some of the answers. The rest of the chapter will cover these concepts in more detail.

We won’t always give solutions to questions in the book. However, if you are ever in doubt about a question, feel free to ask on the course discussion page. You are welcome to share answers to textbook questions since they are practice and not handed in for a grade.

Test Yourself - Primitive vs Reference Type Variables

Answer the following questions to see what you remember about primitive vs reference type variables in Java.

  1. Which of the following is a primitive data type in Java?

    1. String

    2. int

    3. Scanner

    4. Integer

  2. How can you tell a reference type from a primitive type?

  3. What does a reference variable store in Java?

    1. An object and the values of its instance variables

    2. The memory address of an object or null

    3. A pointer to a primitive type

    4. Nothing; it’s just a placeholder

  4. What happens when you assign one reference variable to another (e.g., a = b;) in Java?

    1. A copy of the object is created

    2. The contents of the object are copied

    3. Both variables point to the same object

    4. The original object is deleted

  5. What will be the output of the following Java code?

    public class Test {
        public static void main(String[] args) {
            int x = 5;
            int y = x;
            y = 10;
            System.out.println(x);
        } // main
    } // Test
    
    1. 5

    2. 10

    3. 0

    4. Compilation error

Test Yourself Solution (Don’t open until completing the question above)

Question 1: B

Question 2: Assuming the programmer is following proper Java conventions, reference types will always start with a capital letter. You might also remember the 8 primitive types (byte, short, int, long, float, double, char, boolean) - all others are reference types.

Question 3: B

Question 4: C

Question 5: A