Wrapper Classes in Java Programming
OOPs in Java
In this lesson, we will understand what are Wrapper Classee in Java and how to implement them in programming along with some examples.
What is Wrapper Class in Java?
A wrapper class in Java converts primitive data types, such as (byte, short, int, long, float, double, char, and boolean) into objects. It allows the primitive data type to be used where only objects are accepted, such as in a method that accepts only objects as arguments.
There are 8 wrapper classes in java, and they are:
Wrapper Class | Description |
Boolean | Converts the boolean data type into a Boolean object. Example: Boolean isTrue = new Boolean(true); |
Byte | Converts the byte data type into a Byte object. Example: Byte b = new Byte((byte)127); |
Character | Converts the char data type into a Character object. Example: Character c = new Character('A'); |
Double | Converts the double data type into a Double object. Example: Double d = new Double(3.14); |
Float | Converts the float data type into a Float object. Example: Float f = new Float(2.718f); |
Integer | Converts the int data type into a Integer object. Example: Integer i = new Integer(42); |
Long | Converts the long data type into a Long object. Example: Long l = new Long(123456789); |
Short | Converts the short data type into a Short object. Example: Short s = new Short((short)32767); |
All these classes have methods to convert from primitive to wrapper class and vice versa. Let's see the use of Integer, Long, Float and Double wrapper classes and its different methods.
Use of Integer Wrapper Class
The example below shows how to use the Integer wrapper class and its different methods.
Example
public class Example
{
public static void main(String args[])
{
int x=28,y,z;
String s1="59745", s2;
// Convert primitive integer to Integer object
Integer a = new Integer(x);
System.out.println(a);
// Convert Integer object to primitive integer
y = a.intValue();
System.out.println(y);
// Convert primitive integer to numeric String
s2 = Integer.toString(x);
System.out.println(s2);
// Convert numeric string to primitive integer
z = Integer.parseInt(s1);
System.out.println(z);
// Convert numeric string to Integer object
a = Integer.valueOf(s1);
System.out.println(a);
}
}
Output
28 28 28 59745 59745
Use of Long Wrapper Class
The example below shows how to use the Long wrapper class and its different methods.
Example
public class Example
{
public static void main(String args[])
{
long x=4782,y,z;
String s1="24692", s2;
// Convert primitive long to Long object
Long a = new Long(x);
System.out.println(a);
// Convert Long object to primitive long
y = a.longValue();
System.out.println(y);
// Convert primitive long to numeric String
s2 = Long.toString(x);
System.out.println(s2);
// Convert numeric string to primitive long
z = Long.parseLong(s1);
System.out.println(z);
// Convert numeric string to Long object
a = Long.valueOf(s1);
System.out.println(a);
}
}
Output
4782 4782 4782 24692 24692
Use of Float Wrapper Class
The example below shows how to use the Float wrapper class and its different methods.
Example
public class Example
{
public static void main(String args[])
{
float x=56.24f,y,z;
String s1="745.51f", s2;
// Convert primitive float to Float object
Float a = new Float(x);
System.out.println(a);
// Convert Float object to primitive float
y = a.floatValue();
System.out.println(y);
// Convert primitive float to numeric String
s2 = Float.toString(x);
System.out.println(s2);
// Convert numeric string to primitive float
z = Float.parseFloat(s1);
System.out.println(z);
// Convert numeric string to Float object
a = Float.valueOf(s1);
System.out.println(a);
}
}
Output
56.24 56.24 56.24 745.51 745.51
Use of Double Wrapper Class
The example below shows how to use the Double wrapper class and its different methods.
Example
public class Example
{
public static void main(String args[])
{
double x=18.214,y,z;
String s1="84.3142", s2;
// Convert primitive double to Double object
Double a = new Double(x);
System.out.println(a);
// Convert Double object to primitive double
y = a.doubleValue();
System.out.println(y);
// Convert primitive double to numeric String
s2 = Double.toString(x);
System.out.println(s2);
// Convert numeric string to primitive double
z = Double.parseDouble(s1);
System.out.println(z);
// Convert numeric string to Double object
a = Double.valueOf(s1);
System.out.println(a);
}
}
Output
18.214 18.214 18.214 84.3142 84.3142
Autoboxing and Unboxing
Autoboxing is a process in which primitive data is automatically converted to its corresponding wrapper class object when used in an object-oriented context. For example, the int data type can be automatically converted to an Integer object.
Example
public class Example
{
public static void main(String args[])
{
int x = 82;
Integer y = x; // autoboxing
}
}
Unboxing is the opposite of Autoboxing. It is a process in which a wrapper class object automatically gets converted to its corresponding primitive data type. For example, an Integer object can be automatically converted to an int data type.
Example
public class Example
{
public static void main(String args[])
{
Integer x = new Integer(82);
int y = x; // unboxing
}
}