Data manipulation through Wrapper Classes in Java with examples

In the previous tutorial, we learned about the Static Members in Java class with example. In this tutorial, we will learn about Data manipulation through Wrapper Classes in Java with examples.

295

As we know, Java is said to be nearly 100% object-oriented programming language. So, this means that every entity must be represented by classes and objects in Java. But it is possible to create variables that are primitive and they can be used without creating any class.

For example –

class Example{
public static void main(String args[]){
int a; // declaration of a using primitive data type
}
}

This conflicts with the object-oriented philosophy. That’s why Java is not said to be completely object-oriented. But still, it is said to be nearly 100% object-oriented language.

Wrapper classes in Java allow us to include primitives as an object of the class by using the concept of Wrapper classes.

For every primitive, there is a wrapper class in Java. Using wrapper classes in our programming has a couple of benefits –

  • This supports the concept of objects oriented programming because now, even the primitive variables are used by creating objects
  • Also, we can use some functions that are previously defined in the class that can be used to manipulate the data.

Given below is a list of wrapper classes that are present for the pre-defined data types –wrapper classes in java

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

Data manipulation through Wrapper Classes in Java–

Autoboxing –

Conversion of primitive data type to its corresponding wrapper class automatically is known as autoboxing. This is generally done with the help of valueOf() function. But, in Java 5 it is done implicitly.

We will understand it better with the help of the example given below –

public class WrapperExample1{  
public static void main(String args[]){  
//Converting int into Integer  
int a=20;  
Integer i=Integer.valueOf(a);//converting int into Integer explicitly  
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally  
System.out.println(a+" "+i+" "+j);  
}

Output –

20
20
20

 

 Unboxing –

This is just the vice-versa of what we have learned in Autoboxing. In unboxing, there is an automatic conversion from the wrapper class into its primitive data type. This is generally done with the help of the intValue() function. But in Java 5, this is done implicitly.

public class WrapperExample2{    
public static void main(String args[]){    
Integer a=new Integer(3);    
int i=a.intValue();//converting Integer to int explicitly  
int j=a;//unboxing, now compiler will write a.intValue() internally    
System.out.println(a+" "+i+" "+j);    
}}

Output –

3 3 3

In the example below, we will understand the use of Wrapper Class in a much detailed way –

public class Wrapper{  
public static void main(String args[]){  
byte b=10;  
short s=20;  
int i=30;  
long l=40;  
float f=50.0F;  
double d=60.0D;  
char c='h';  
boolean b2=true;  
  

//Autoboxing
Byte byteobj=b;  
Short shortobj=s;  
Integer intobj=i;  
Long longobj=l;  
Float floatobj=f;  
Double doubleobj=d;  
Character charobj=c;  
Boolean boolobj=b2;  
  

//Printing objects  

System.out.println("Object Values-");  
System.out.println("Byte object: "+byteobj);  
System.out.println("Short object: "+shortobj);  
System.out.println("Integer object: "+intobj);  
System.out.println("Long object: "+longobj);  
System.out.println("Float object: "+floatobj);  
System.out.println("Double object: "+doubleobj);  
System.out.println("Character object: "+charobj);  
System.out.println("Boolean object: "+boolobj);  
  

//Unboxing

byte bytevalue=byteobj;  
short shortvalue=shortobj;  
int intvalue=intobj;  
long longvalue=longobj;  
float floatvalue=floatobj;  
double doublevalue=doubleobj;  
char charvalue=charobj;  
boolean boolvalue=boolobj;  

  

System.out.println("Primitive Values");  
System.out.println("byte value: "+bytevalue);  
System.out.println("short value: "+shortvalue);  
System.out.println("int value: "+intvalue);  
System.out.println("long value: "+longvalue);  
System.out.println("float value: "+floatvalue);  
System.out.println("double value: "+doublevalue);  
System.out.println("char value: "+charvalue);  
System.out.println("boolean value: "+boolvalue);  

}}

Output –

object values –
Byte object : 10
Short Object : 20
Integer object : 30
Long object : 40
Float object : 50.0
Double object : 60.0
Character object : h
Boolean object : true
 

Primitive values – 
Byte value : 10
Short value : 20
Int value : 3
Long value : 40
Float value : 50
Double value : 60.0
Char value : h
Boolean value : true

So, in this article, we learned about the wrapper classes, and their use for getting more towards the object-oriented style of programming. And also in the manipulation of data.

Related Quiz:

Previous QuizBasics Programming Model and Application Instruction set By Dr. Omkar Pattnaik, Sandip University
Next QuizHow to Create and use Packages in Java

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.