by BehindJava

What are Primitive and Wrapper Classes in Java

Home » java » What are Primitive and Wrapper Classes in Java

In this tutorial we are going to learn about Primitive and Wrapper Classes in Java.

Java contains mainly 8 primitive types. For all primitive types there is a Wrapper classes (Object classes) also available inside java.lang package. Below table shows all primitive types and corresponding wrapper classes.

Primitive Types Wrapper Classes
byte java.lang.Byte
short java.lang.Short
int java.lang.Integer
long java.lang.Long
float java.lang.Float
double java.lang.Double
char java.lang.Character
boolean java.lang.Boolean

When we use Primitive? When we use Wrapper?

Due to java’s Autoboxing and UnBoxing features it is very very rare chance that we need to cast the primitive class to corresponding wrapper class .So both primitive and wrapper classes are almost same.

The main case that the Wrapper classes (Object classes) are useful as compared with Primitive types is that in the case of variable value is not known. If the variable value is not knows it is needed to pass null value to that corresponding variable, it is only possible in the case of Wrapper classes.

Integer i = null; // possible
int i = null; // not possible because only Object variables can be null

Also in java api the wrapper classes are introduced mainly for.

  1. To provide a machanism for “wrap” primitive values in an object so that the primitives can be included in activities reserved for objects, like as being added to Collections, or returned from a method with an object return value.
  2. To provide some addtional utility function to the primitive. Most of these additional utility functions are for some conversions like converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.

What about Void?

Void is not a Wrapper Class. But the Void class is similar in that it provides an object representation of the void return. Void is mainly used by the java.lang.reflect API hold a reference to the Class object representing the Java keyword void because It contains Void.TYPE, useful for testing return type with reflection.

public void foo() {}
if (getClass().getMethod("foo").getReturnType() == Void.TYPE) ...

Default Values of Primitive Types

The following program and its output tells the answer of above heading.

public class DefaultValues {
static boolean bool;
static byte by;
static char ch;
static double d;
static float f;
static int i;
static long l;
static short sh;
static String str;
public static void main(String[] args) {
     System.out.println("boolean :" + bool);
     System.out.println("byte :" + by);
     System.out.println("char :" + ch);
     System.out.println("double :" + d);
     System.out.println("float :" + f);
     System.out.println("int :" + i);
     System.out.println("long :" + l);
     System.out.println("short :" + sh);
     System.out.println("String :" + str);


boolean :false
boolean :false
byte :0
char :
double :0.0
float :0.0
int :0
long :0
short :0
String :null