Primitive data types are not objects, but sometimes, objects are needed.

Primitive typeWrapper class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

What is the difference? Primitive data types are a type inherited from programming languages that are not object-oriented, such as C.

In addition, primitive data types do not need to be instantiated; that is, we can declare them directly. For example, we can initialize a primitive type as follows:

int numero = 0;

And we can also use the Integer class, in which case we will need to create the object:

Integer numero = new Integer(0);

Wrappers, known as wrapper classes, are special classes that provide basic functions to primitive types. Additionally, these classes have methods that allow manipulating the primitive data type as if it were an object.

There are several terms used when we use wrappers:

  • Boxing: Converting a primitive type into its Wrapper class.
  • Unboxing: Converting an object of a Wrapper class into its primitive type.

Among the main functions shared by these classes are:

  • xxxValue(): Takes a numeric value and converts it to a primitive type:
Integer numero = new Integer(13);
double n = numero.doubleValue();
  • valueOf(), parseXxx(): Convert a string to the specified primitive type or throw a NumberFormatException exception if it is not possible to do so.
String a = new String("113");
Integer b = Integer.valueOf(a);
  • toString(): Converts primitive data types into character strings.
Integer a = new Integer(12);
String b = a.toString();