Video lecture
Watch on YouTube(opens in a new tab)Sub topic 2
Primitive and Reference Types
A Java variable holds a value. Its declared type tells Java what kind of value it can hold.
Java types fall into two broad groups: primitive types and reference types. The difference matters most when you copy a variable. A primitive variable holds a simple value. A reference variable holds a reference: a value that lets Java find an object.
An object is a piece of data that can have its own values and actions. A class is a definition for a kind of object. For example, Java’s String class describes objects that hold text.
The two groups
| Group | What a variable holds | Examples |
|---|---|---|
| Primitive type | A simple value | `int`, `char`, `boolean` |
| Reference type | A reference to an object, or `null` | `String`, `Counter`, arrays |
Java has eight primitive types:
| Primitive type | Kind of value |
|---|---|
| `byte` | A whole number |
| `short` | A whole number |
| `int` | A whole number |
| `long` | A whole number |
| `float` | A number that can have a decimal part |
| `double` | A number that can have a decimal part |
| `char` | A single unit of text |
| `boolean` | `true` or `false` |
These types have different uses and limits. For now, focus on their shared feature: a primitive variable holds its own value.
String is a reference type, not a primitive type. An array is also an object. An array holds a fixed number of values, and an array variable holds a reference even when the array contains primitive values.
What happens when you copy a variable?
When you assign one variable to another, Java copies the value in the first variable.
For a primitive variable, that value is the primitive value itself. Changing one variable afterward does not change the other.
For a reference variable, the copied value is the reference, not the object. Both variables can then refer to the same object. If that object can be changed, a change made through one reference can be seen through the other.
The diagram shows the result of the program in the next section.
The arrows show references. They do not mean that one variable contains the other variable.
Worked example: copying values and references
Save this program in a file named TypeDemo.java.
class Counter {
int value;
}
class TypeDemo {
public static void main(String[] args) {
int score = 3;
int scoreCopy = score;
scoreCopy = 4;
Counter first = new Counter();
first.value = 3;
Counter second = first;
second.value = 4;
System.out.println("score: " + score);
System.out.println("scoreCopy: " + scoreCopy);
System.out.println("first.value: " + first.value);
System.out.println("second.value: " + second.value);
}
}With the Java development tools installed, run these commands in the folder containing the file. javac compiles the program, which means it translates the source code into a form Java can run.
javac TypeDemo.java
java TypeDemoThe output is:
score: 3
scoreCopy: 4
first.value: 4
second.value: 4Follow the primitive values
The program gives score the primitive value 3. It then copies that value into scoreCopy.
When scoreCopy changes to 4, score stays 3. The variables hold separate values.
Follow the references
The Counter class defines an object with a field named value. A field is a variable that belongs to an object. The expression new Counter() creates a Counter object, and first receives a reference to it.
The assignment second = first copies that reference. It does not create another Counter object. Both first and second now refer to the same object.
The line second.value = 4 changes the object’s field. Reading that field through first also gives 4, because there is only one Counter object in this example.
References can be null
A reference variable can also hold null. This special value means the variable does not refer to an object.
You can compare a reference with null before trying to use the object. This matters because trying to access a field or call a method through a null reference causes a NullPointerException. An exception is a problem reported while the program runs.
A method is an action that code can call. For example, a String object has a method named length that reports the length of its text. The next example checks for null before calling it.
Comparing objects and text
The operator == compares values. With primitive variables, it compares their primitive values. With reference variables, it checks whether the references point to the same object. It does not generally check whether two objects contain the same data.
To compare the text in two String objects, use the equals method. Two separate String objects can hold the same text.
Save this program as TextDemo.java:
class TextDemo {
public static void main(String[] args) {
String first = new String("Java");
String second = new String("Java");
System.out.println(first == second);
System.out.println(first.equals(second));
String shared = first;
first = "Python";
System.out.println(shared);
String missing = null;
if (missing != null) {
System.out.println(missing.length());
} else {
System.out.println("No text");
}
}
}Run it with:
javac TextDemo.java
java TextDemoThe output is:
false
true
Java
No textThe two new String expressions create separate objects. So first == second is false, even though first.equals(second) is true: both objects hold the same text.
The example uses new String to make the separate objects clear. You normally do not need it to write text in Java. Also, Java may share String objects for text written in code, so do not use == to test whether their text matches.
The variable shared receives a copy of the reference in first. Then first is given a reference to "Python". This does not change shared; it still refers to the "Java" object.
A String is immutable, which means its text cannot be changed after the object is created. This is different from the Counter object, whose value field the earlier program changed.
Finally, missing holds null. The condition is false, so the program prints No text instead of calling length through a null reference.
Check your understanding
Exercise
If int a = 7; int b = a; is followed by b = 9;, what value does a hold?
Show answer
a still holds 7. The value was copied, so changing b does not change a.
Exercise
Two variables refer to the same Counter object. One changes its value field to 8. What will the other see when it reads that field?
Show answer
It will see 8. Both references point to the same object.
Exercise
Two separate String objects both hold the text "cat". Which test should you use to compare their text: == or equals?
Show answer
Use equals. For references, == checks whether they point to the same object, not whether their text matches.