What are the differences between Java primitive types vs boxed primitives?

What are variable types in Java

Every variable in Java has a type. There are two kinds of types for variables. They are:

  • Primitive types
  • Reference types

What are primitive types in Java

Primitive types directly contain values. There are 8 primitive types:

  • byte
  • short
  • int
  • long
  • char
  • float
  • double
  • boolean

What are reference types in Java

Reference types are references to objects. All reference types are a subclass of type java.lang.Object Following are 5 Java reference types.

  • Annotation
  • Array
  • Class
  • Enumeration
  • Interface

Each of the primitive types in Java also has an equivalent reference type. For example, int has Integer, long has Long, boolean has Boolean, and so on. These reference types are called Boxed primitives.

Primitive types vs. Boxed Primitives

Primitive Types
Boxed Primitives
It supports only functional values It supports functional values and null
It is time and space efficient It is less time and space efficient than primitive types
As it only supports values. So, 100 == 100 and efficient As it has values and reference. new Integer(100) != new Integer(100). So avoid using == on boxed primitives
It can not be used as a argument for a generic type variable It can be used as a argument for a generic type variable