Java: Primitive vs. Reference Type

Every data type within the Java language can be categorized as a primitive data type or a reference data type. As discussed in a prior lesson, there are eight primitive types in Java (byte, short, int, long, float, double, char, and boolean). Every other data type in this section is a reference type. Two examples of reference types were already discussed: Strings and Arrays. 

The primary difference between a primitive type and a reference type is the data that is stored.

A primitive type stores its own data.

For example, when programmers write this:

int myExample = 3;

The variable myExample stores the actual value 3.

Conversely, instead of storing the actual value, a reference type stores a reference to the data. The compiler is not informed about the data and instead learns about where the data can be found.  

Suppose:

String message = "Perfect";

The string “Perfect” is not stored in the variable message.

Conversely, the string “Perfect” is stored at some other location in the computer’s memory, and the message only stores the path to this location.  

For now, this is all that will be covered about reference types. Since this lesson is aimed at beginners, this topic or reference types’ significance will not be discussed further. Just keep in mind the primary difference between primitive types and reference types.

Leave a Reply

%d bloggers like this: