What’s the difference between a primitive type and a class type in Java?

Every variable in Java has a type, which essentially tells Java how that variable should be treated, and how much memory should be allocated for that variable. Java has basic types for characters, different kinds of integers, and different kinds of floating point numbers (numbers with a decimal point), and also types for the values true and false – char, int, float, and bool. All of these basic types are known as primitive types.

What is a class type in Java?

Java classes, as you probably already know, are created to solve object oriented problems. Any object of a class has the type “class”. Because an object of a class is more complex than a simple integer, boolean, or other “primitive” type, a variable naming an object is known to be a class type.

Class types vs Object types vs Reference types

You might be confused by all the different terminology used. So just to clarify, class types, object types, and reference types all mean the exact same thing – an object of a class. In this discussion, we will just be using the term class types to refer to objects of a class.

The differences between class and primitive types in Java

A variable of a class type – like a String – stores objects of its class differently from how variables of primitive types – like int or char – store their values. Every variable, whether it’s of a primitive type or of a class type, is implemented as a location in computer memory. For a variable of a primitive type, the value of the variable is stored in the memory location assigned to the variable. So, if an integer variable is declared as “int x = 3”, then when we look at the memory location of “x”, there will be a “3” stored there just as expected.

However, a variable of a class type only stores the memory address of where the object is located – not the values inside the object. So, if we have a class called “SomeClass”, when we create an object like this: “SomeClass anObject”, then when we look at “anObject” in memory, we will see that it does not store any of the variables that belong to that object in memory. Instead, the variable “anObject” just stores an address of another place in memory where all the details of “anObject” reside. This means that the object named by the variable is stored in some other location in memory and the variable contains only the memory address of where the object is stored. This memory address is called a reference to the object. If this is confusing, you will see an example of this further down.

Different memory requirements for variables of class types and primitive types

A value of a primitive type – like a type int – will always require the same amount of memory to store one value. There is a maximum value of type int – which means that values of type int will have a limit on their size. But – the big difference between a primitive type and a class type is that an object of a class type, like an object of the class String, can be of any size. The memory location for a variable of type String (a class type) is of a fixed size, so it cannot store a gigantic string like one that is 3,000 characters long. But, it can and does store the address of any string since there is a limit to the size of an address. So, remember that a variable of a class type stores an address of the object, and not the object values themselves – which are stored at that address in memory.

Two class type variables may contain same reference (memory address)

Because variables of a class type contain a reference (memory address), two different variables can hold the same reference, and in that situation both variables name the same object. Any change to the object named by one of these variables will produce a change to the object named by the other variable, since they both reference the same object. This is best shown by an example, so take a look at the following simple code, assuming we have a class called PersonClass which has a method called set that just sets the name and age for a given Person object. This code can be understood without looking at the definition of PersonClass, which is also presented below for convenience:

PersonClass variable1 = new PersonClass("Mary", 32);
PersonClass variable2;
// Both variable2 and variable1 now both name the same object
variable2 = variable1; 

/*this also changes variable1, since variable 2 and variable1
   name the same exact object */

variable2.set("Jack", 22);

System.out.println(variable1);

And the output of this code is:

Jack 22

In the code above, you can see that when we made a change to variable2, that it also changed variable1. This is because both variable1 and variable2 reference the same object in memory – and this happened when we ran this line of code “variable2 = variable1”. When we use the assignment operator (the “=”) with variables of a class type, then we are assigning a reference – in other words variable2 is given the memory address (or reference) of variable1. Remember that the word reference means the same thing as memory address. This assignment of references is best displayed by actual drawings, which we provide below so that you can actually visualize what’s going on when an object is assigned to another in Java :

And here is the definition of the class PersonClass:

public class PersonClass
{
  private String name;
  private int number;

  public PersonClass(String initialName, int initialNumber)
  {
    name = initialName;
    number = initialNumber; 
  }  

  public void set(String newName, int newNumber)
  {
    name = newName;
    number = newNumber;
  }

} 

Generally, when you look at a declaration like this: “PersonClass y”, then you would refer to “y” as an object. Although, for the sake of keeping things simple when communicating it is good, but in reality “y” is a variable that stores an address. That address is where the object is really stored, not in the variable “y”. This is an important distinction, and something that you should understand.

Class types are also reference types

A type whose variables contain references is called a reference type. This means that in Java, class types are also reference types. But, primitive types (like ints, booleans, etc.) are not reference types since primitive type variables do not hold references (which are basically addresses).

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

14 thoughts on “Difference between a primitive type and a class type?”

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.