Java Primitive Types vs Wrapper Classes

The Primitive Wrapper Class

A primitive wrapper class is one of eight classes provided in the java.lang package to provide object methods for the eight primitive types. All of the primitive wrapper classes in Java are immutable whose state cannot be modified after they are created.

The following table lists the primitive types and their corresponding wrapper classes:

Primitive Type Wrapper Class Constructor Arguments
byte Byte byte or String
short Short short or String
int Integer int or String
long Long long or String
float Float float, double or String
double Double double or String
char Character char
boolean Boolean boolean or String

When To Use Primitive?

Most of the time you want to use the primitive types in stead of their wrapper classes in your code for couple of reasons:

Make Your Code Simple, Easy To Read and Avoid Mistake

To compare 2 operands in primitive type; we use == operator. For wrapper object, we use equal() method instead. And this is a very common mistake.

And here’s the output:

Performance

It is slower to use the wrapper classes compared to the primitives. Because object instantiation, method calls… cost some extra resources.

When To Use Objects in place of Primitives?

There are three reasons that you might use objects in place of primitives object:

  1. As an argument of a method that expects an object (often used when manipulating collections of numbers).
  2. To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
  3. To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).

Autoboxing and Unboxing

Autoboxing

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on.

The Java compiler applies autoboxing when a primitive value is:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.

Consider the following code:

The code at line 10 is converted at runtime by the compiler:

Unboxing

Converting an object of a wrapper type to its corresponding primitive value is called unboxing.

The Java compiler applies unboxing when an object of a wrapper class is:

  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Let analyze the following code:

Line 9 list.add(3.141592654);  the PI value (double) is autoboxed (primitive to object) through method invocation to add in the list object.

Line 11 double pi = list.get(0); the PI value is unboxed (object to primitive) through assignment.

Using Generics Type

Generics type doesn’t support primitive types so definitely you have to use their wrapper classes.

Code that uses generics has many benefits over non-generic code:

  • Stronger type checks at compile time.
    A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.
  • Elimination of casts.
    The following code snippet without generics requires casting:

 

When re-written to use generics, the code does not require casting:

  • Enabling programmers to implement generic algorithms.
    By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.

Run The Applications

To run the applications; right click to each of them; select Run As -> Java Application.