Wrapper Class in Java

The primitive data types byte, short, int, long, float, double, char and boolean are not objects, they do not belong to any class.

Many data structures like Collection in JDK 1.4 on word accept only objects. In this case, there is a need to convert primitive data types into objects.

Wrapper classes allow primitive data types to convert into objects. As the name says, a wrapper class wraps around a primitive data type and gives it an object representational form.

They are one for primitive type Boolean, Byte, Character, Double, Float, Integer, Long and Short. Wrapper classes make the primitive type data to act as objects. Wrapper classes belong to Java.lang package.

Wrapper classes are immutable, means once they have been given a value that value cannot be changed.

int I = 5;
Integer obj = new Integer (i); // wrapping
int j = obj.intValue(); // unwrapping

double d = 5.5;
Double obj  = new Double (d); // wrapping
Double dd = obj.doubleValue();// unwrapping

Method xxxValue() is used to unwrap an object.

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

Parsing String into primitive data types

  • Wrapper classes are used to parse String into primitive data types.
  • Method parsXxx() is used to pars a string.

Convert primitive data into String

  • String class can be used to convert primitive data types into a string.
  • Static method valueOf () is used to convert primitive data into strings.

Look at the below program:

public class Demo {

	public static void main(String[] args) {

		// converting a string value in integer
		String intValue = "25";
		int i = Integer.parseInt(intValue);
		System.out.println("integer value is - " + i);

		// converting String value in double
		String doubleValue = "60.75";
		double d = Double.parseDouble(doubleValue);
		System.out.println("double value is - " + d);

		// converting String value in boolean
		String booleanValue = "true";
		boolean b = Boolean.parseBoolean(booleanValue);
		System.out.println("boolean value is - " + b);

		// converting String value in float
		String floatValue = "15.23";
		float f = Float.parseFloat(floatValue);
		System.out.println("float value is - " + f);


		// converting integer value in String
		intValue = String.valueOf(i);

		// converting double value in String
		doubleValue = String.valueOf(d);

		// converting boolean value in String
		booleanValue = String.valueOf(b);

		// converting Float value in String
		floatValue = String.valueOf(f);

		System.out.println("integer value in String is - " + intValue);
		System.out.println("double value in String is - " + doubleValue);
		System.out.println("boolean value in String is - " + booleanValue);
		System.out.println("float value in String is - " + floatValue);

Example Output

Boxing and UnBoxing

Java 1.5 introduced a new feature ‘Boxing and Unboxing’ with their release of the collection framework.

Converting primitive data type into a Java object is called Boxing. Java does boxing automatically.

Converting a Java Wrapper class object into its primitive data type is called Unboxing.

public class Demo {

	public static void main(String[] args) {

		int i = 25;
		Integer iObj = i;
		int j = iObj;
		System.out.println("the integer value of the object - " + j);

		double d = 12.5;
		Double dObj = d;
		double dd = dObj;
		System.out.println("double value of object - " + dd);

Example Output