Java Wrapper Classes Explained

What exactly is a Java Wrapper Class? We are very familiar with primitive data types like int, short, long, float, etc. Each of the primitive data types is associated with a wrapper class. You are essentially wrapping a class around a primitive data type. The table below shows the primitive data type and its wrapper class counterpart.

Primitive Data Type Wrapper Class
boolean Boolean
byte Byte
char Char
int Integer
float Float
double Double
long Long
short Short

Wrapper Class Purpose

The wrapper class in Java provides the ability to convert a primitive data type into an object. You can also convert an object back into a primitive data type. These two processes are defined as autoboxing and unboxing.

Take a look at the Java code samples below that first define the primitive data type and then the Wrapper Class that will store the primitive as an Object.

//JavaCodeDepot Sample

boolean jcdBoolean = true;
Boolean jcdBooleanWrapper = new Boolean(jcdBoolean);
byte jcdByte = 127;
Byte jcdByteWrapper = new Byte(jcdByte);
char jcdChar = 'J';
Character jcdCharWrapper = new Character(jcdChar);
double jcdDouble = 1.7777;
Double jcdDoubleWrapper = new Double(jcdDouble);
float jcdFLoat = 3.4028234f;
Float jcdFloatWrapper = new Float(jcdFLoat);
int jcdInt = 777777;
Integer jcdIntWrapper = new Integer(jcdInt);
long jcdLong = 922337277777L;
Long jcdLongWrapper = new Long(jcdLong);
short jcdShort = 31777;
Short jcdShortWrapper = new Short(jcdShort);

Wrapper Class Autoboxing Sample Code

Converting a primitive data type to a class object is what is known as autoboxing.


public class JavaWrapperClass 

	public static void main(String[] args) 
		//Let's define an integer and give a value of 100  
		int jcdInt = 100;  
		//Let's wrap the integer in the Integer Class
		Integer jcdIntegerClass1 = Integer.valueOf(jcdInt);
		//Let's define a new Integer Class and set it to jcdInt 
		Integer jcdIntegerClass2 = jcdInt;
		System.out.println("jcdInt: "+jcdInt); 
		System.out.println("jcdIntegerClass1: "+jcdIntegerClass1);
		System.out.println("jcdIntegerClass2: "+jcdIntegerClass2);


Wrapper Class Autoboxing Output

jcdInt: 100
jcdIntegerClass1: 100
jcdIntegerClass2: 100

Wrapper Class Unboxing Sample Code

Converting a class object to a primitive data type what is known as unboxing.


public class JavaWrapperClass 

	public static void main(String[] args) 
		//Let's define an Integer Class with a value of 100
		Integer jcdIntegerClass3 = new Integer(100);    
		//Let's convert the Integer Class to a primitive data type
		int jcdInt1 = jcdIntegerClass3.intValue();  
		//Le'ts create a new primitive and assign it the 
		int jcdInt2 = jcdIntegerClass3;
		System.out.println("jcdIntegerClass3: "+jcdIntegerClass3);
		System.out.println("jcdInt1: "+jcdInt1);
		System.out.println("jcdInt2: "+jcdInt2);


Wrapper Class Unboxing Output

jcdIntegerClass3: 100
jcdInt1: 100
jcdInt2: 100

Add a Comment

Your email address will not be published. Required fields are marked *