
Java Wrapper Class: Definition and Examples
Table of Content:
As you know, Java uses eight primitive types (also called simple types), such as boolean, char, byte, short, int, long, float, double to hold the basic data types supported by the language.
Example
boolean = true ; int i = 5000; float gpa = 13.65f; double rr = 54.44; etc.
However, in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper classes.
Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.
The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean. These classes offer a wide array of methods that allow you to fully integrate the primitive types into Java’s object hierarchy. Each is briefly examined next.
The above eight classes of java.lang package are known as wrapper classes in java. The list of eight wrapper classes are given below:
Primitive Type | Wrapper class |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
autoboxing and unboxing feature converts primitive into an object and object into primitive automatically. The automatic conversion of primitive into an object is known as autoboxing and vice-versa unboxing.
The object of the wrapper class contains or wraps its respective primitive data type. Converting primitive data types into object is called boxing, and this is taken care by the compiler. Therefore, while using a wrapper class you just need to pass the value of the primitive data type to the constructor of the Wrapper class.
Character is a wrapper around a char. The constructor for Character is
Character(char ch)
Autoboxing
Beginning with JDK 5, Java added two important features: autoboxing and auto-unboxing. Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. There is no need to explicitly construct an object.
Auto-unboxing is the process by which the value of a boxed
object is automatically extracted (unboxed) from a type wrapper when its value is needed.
There is no need to call a method such as intValue( )
or doubleValue( )
.
Integer marks = 100; // autobox an int
Here is the modern way to construct an Integer object that has the value 100. Notice that no object is explicitly created through the use of new. Java handles this for us, automatically.
Unboxing
To unbox an object, simply assign that object reference to a primitive-type variable. For example, to unbox iOb, you can use this line:
int i = marks; // auto-unbox
Here is the preceding program rewritten to use autoboxing/unboxing:
Program:
// Demonstrate autoboxing/unboxing. class AutoBox { public static void main(String args[]) { Integer marks = 100; // autobox an int int i = marks; // auto-unbox System.out.println(i + " " + marks); // displays 100 100 } }
Program:
100 100 Press any key to continue . . .
Primitive to Wrapper conversion
Program:
public class WrapperExample1{ public static void main(String args[]){ //Converting int into Integer int a=15; Integer i=Integer.valueOf(a);//converting int into Integer //autoboxing, now compiler will write Integer.valueOf(a) internally Integer k=a; System.out.println(a+" "+i+" "+k); } }
Program:
15 15 15 Press any key to continue . . .
Wrapper to Primitive conversion
Program:
public class WrapperExample1{ public static void main(String args[]){ //Converting Integer to int Integer a=new Integer(15); int i=a.intValue();//converting Integer to int //unboxing, now compiler will write a.intValue() internally int j=a; System.out.println(a+" "+i+" "+j); } }
Program:
15 15 15 Press any key to continue . . .
Example of boxing and unboxing ?
Program:
public class wrapper { public static void main(String args[]) { Integer s = 12; // boxes int to an Integer object s = s + 20; // unboxes the Integer to a int System.out.println(s); } }
Program:
32 Press any key to continue . . .
When s is assigned an integer value, the compiler boxes the integer because s is integer object. Later, s is unboxed so that they can be added as an integer.
- Question 1: Why is a class known as composite data type?
Related Questions
- Assignment 1: Boolean Wrapper class in Java
- Assignment 2: Character Wrapper class in Java
- Assignment 3: Byte Wrapper class in Java
- Assignment 4: Byte Wrapper class in Java Example 2
- Assignment 5: Short Wrapper class in Java
- Assignment 6: Short Wrapper class in Java Example 2
- Assignment 7: Number Class in java, Boxing and un-boxing
- Assignment 8: Conversion Integer Number Object to primitive data type byte
- Assignment 9: Conversion Integer Number Object to primitive data type double
- Assignment 10: Conversion Integer Number Object to primitive data type long
- Assignment 11: Integer Wrapper class in Java
- Assignment 12: Long Wrapper class in Java
- Assignment 13: Float Wrapper class in Java
- Assignment 14: Double Wrapper class in Java
- Assignment 15: Conversion Integer Number Object to primitive data type short
- Assignment 16: Conversion Integer Number Object to primitive data type int
- Assignment 17: Conversion (wrapper class)Integer Number Object to (primitive data type) int
- Assignment 18: Conversion (wrapper class) Float Number Object to (primitive data type) float
- Assignment 19: Conversion (wrapper class) Float Number Object to (primitive data type) int
- Assignment 20: Conversion (wrapper class) Float Number Object to (primitive data type) byte
- Assignment 21: Conversion (wrapper class) Float Number Object to (primitive data type) short
- Assignment 22: Conversion (wrapper class) Float Number Object to (primitive data type) long
- Assignment 23: Conversion (wrapper class) Float Number Object to (primitive data type) double
- Assignment 24: Conversion (wrapper class) Boolean Number Object to (primitive data type) boolean
- Assignment 25: Conversion (wrapper class) Character Number Object to (primitive data type) char
- Assignment 26: Conversion (wrapper class) Byte Number Object to (primitive data type) byte
- Assignment 27: Conversion (wrapper class) Byte Number Object to (primitive data type) short
- Assignment 28: Conversion (wrapper class) Byte Number Object to (primitive data type) int
- Assignment 29: Conversion (wrapper class) Byte Number Object to (primitive data type) long
- Assignment 30: Conversion (wrapper class) Byte Number Object to (primitive data type) float
- Assignment 31: Conversion (wrapper class) Byte Number Object to (primitive data type) double
- Assignment 32: Conversion (wrapper class) Short Number Object to (primitive data type) short
- Assignment 33: Conversion (wrapper class) Short Number Object to (primitive data type) byte
- Assignment 34: Conversion (wrapper class) Short Number Object to (primitive data type) int
- Assignment 35: Conversion (wrapper class) Short Number Object to (primitive data type) long
- Assignment 36: Conversion (wrapper class) Short Number Object to (primitive data type) float
- Assignment 37: Conversion (wrapper class) Short Number Object to (primitive data type) double
- Assignment 38: Conversion (wrapper class) Long Number Object to (primitive data type) long
- Assignment 39: Conversion (wrapper class) Long Number Object to (primitive data type) int
- Assignment 40: Conversion (wrapper class) Long Number Object to (primitive data type) float
- Assignment 41: Conversion (wrapper class) Long Number Object to (primitive data type) byte
- Assignment 42: Conversion (wrapper class) Long Number Object to (primitive data type) short
- Assignment 43: Conversion (wrapper class) Double Number Object to (primitive data type) short
- Assignment 44: Conversion (wrapper class) Double Number Object to (primitive data type) byte
- Assignment 45: Conversion (wrapper class) Double Number Object to (primitive data type) float
- Assignment 46: Conversion (wrapper class) Double Number Object to (primitive data type) int
- Assignment 47: Conversion (wrapper class) Double Number Object to (primitive data type) double
- Assignment 48: Conversion (wrapper class) Double Number Object to (primitive data type) long
- Assignment 49: Conversion (primitive data type) int to (wrapper class) Integer Number Object using valueof() method
- Assignment 50: Conversion (primitive data type) int to (wrapper class) Double Number Object using valueof() method
- Assignment 51: Conversion (primitive data type) int to (wrapper class) Float Number Object using valueof() method
- Assignment 52: Conversion (primitive data type) int to (wrapper class) Integer Number Object using valueof() method
- Assignment 53: Conversion (primitive data type) int to (wrapper class) Integer Number Object using valueof() method
- Assignment 54: Conversion (primitive data type) int to (wrapper class) Integer Number Object using valueof() method, binary to decimal
- Assignment 55: Conversion (primitive data type) int to (wrapper class) Integer Number Object using valueof() method, hexadecimal to decimal
- Assignment 56: Wrapper class Wrapping and Unwrapping
- Assignment 57: Wrapper class Wrapping and Unwrapping example 2
- Assignment 58: Wrapping data in Wrapper class