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.


System.out.println("Enter an Integer:");
int n=KB.nextInt();
Integer I=new Integer(n); //Boxing : Creating an Integer Object
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.


Integer I=new Integer(n);
inti=I; //Unboxing : Converting Object to Primitive type
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);

		//long-		Long

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

		//float- 	Float

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

		//char-		Character

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

		//double-	Double

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



Enter an Integer:
Enter a Long Integer:
Enter a Float Value:
Enter a Character:
Enter a Double:

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.