C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Wrapper Classes in Java with Example

Learn: Wrapper Classes in Java - in this article we will be learning about the introduction of Wrapper Classes, Why they are used? And Why they were added in Java in the first place?
Submitted by Mayank Singh, on June 20, 2017

As we know,
Java is an Object-Oriented language, i.e. it closely follows the principles of Classes and Objects, but it is also true that Java is not 100% OOP Language, reason is that still Java uses Primitive data types such as int, char, float, long, double, etc. A need was felt to convert these Primitive Data Types into Classes and Objects, thus Java introduced a concept known as Wrapper Classes.

The Objects of Wrapper Classes wraps the Primitive data types, this comes in handy when we need to use more methods on a primitive data type like for example suppose we want to convert a Non-String Object to String type we use toString() method , the toString() method will return the String representations of the Objects. Similarly, we can have many other examples.

Coming back to Java's Wrapper Classes, let’s see what are the available Wrapper Classes in Java.

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

Let's us discuss two concepts related to Wrapper Classes, these are pretty straight forward:

1) Boxing

Conversion of a Primitive Data type to Corresponding Object is known as Boxing, it is handled by the Compiler by the help of Constructors.

Example:

System.out.println("Enter an Integer:");
int n=KB.nextInt();
Integer I=new Integer(n); //Boxing : Creating an Integer Object
System.out.println(I);
Input: 8
Output: 8

2) Unboxing

It can be considered as opposite to Boxing, when the Object needs to be converted back into corresponding primitive data type, it is then known as Unboxing.

Example:

Integer I=new Integer(n);
inti=I; //Unboxing : Converting Object to Primitive type
System.out.println(i);
Input: 8
Output: 8

Consider the program:

import java.util.*;

class Wrapper
{
	public static void main(String args[])
	{
		Scanner KB=new Scanner(System.in);

		//int-		Integer

		System.out.println("Enter an Integer:");
		int n=KB.nextInt();
		Integer I=new Integer(n);
		System.out.println(I);

		//long-		Long

		System.out.println("Enter a Long Integer:");
		long l=KB.nextLong();
		Long L=new Long(l);
		System.out.println(L);

		//float- 	Float

		System.out.println("Enter a Float Value:");
		float f=KB.nextFloat();
		Float F=new Float(f);
		System.out.println(F);

		//char-		Character

		System.out.println("Enter a Character:");	
		char c=KB.next().charAt(0);
		Character C=new Character(c);
		System.out.println(C);

		//double-	Double

		System.out.println("Enter a Double:");
		Double d=KB.nextDouble();
		Double D=new Double(d);
		System.out.println(D);

	}
}

Output

Enter an Integer:
8
8
Enter a Long Integer:
1234567898745
1234567898745
Enter a Float Value:
8.8
8.8
Enter a Character:
c
c
Enter a Double:
12.55
12.55








COMMENTS