Wrapper Class is used for converting primitive data type into object and object into a primitive data type. For each primitive data type, a pre-defined class is present which is known as Wrapper class. From J2SE 5.0 version the feature of autoboxing and unboxing is used for converting primitive data type into object and object into a primitive data type automatically.
Use Wrapper Classes :Java when input is given by the user, it is in the form of String. To convert a string into different data types, Wrapper classes are used. We can use wrapper class each time when want to convert primitive to object or object to primitive. All the primitive wrapper classes are immutable. When a new object is created the old object is not modified.
List of Wrapper Classes in Java :
Byte | Short | Integer | Long | Float | Double | Character | Boolean |
Convert Integer Primitive Type to Integer Wrapper Object
package com.jaladhi.jst;public class IntegerWrapperClass {
public static void main(String args[]) {
int num = 1000;
Integer obj = Integer.valueOf(num);
System.out.println("Primitive Integer Number : " + num);
System.out.println("Wrapper Integer Number : " + obj);
}
}
Converted Integer Number : 1000
Convert Integer Wrapper Object To Integer Primitive Type
package com.jaladhi.jst;public class IntegerPrimitive {
public static void main(String[] args) {
Integer obj = new Integer(1000);
int num = obj.intValue();
System.out.println("Wrapper Object Value : " + obj);
System.out.println("Primitive Integer Value : " + num);
}
}
Primitive Integer Value : 1000
Convert Double Primitive Type to Double Wrapper Object
package com.jaladhi.jst;public class DoubleWrapperClass {
public static void main(String args[]) {
double num = 15.65;;
Double obj = Double.valueOf(num);
System.out.println("Primitive Double Number : " + num);
System.out.println("Wrapper Double Number : " + obj);
}
}
Converted Double Number : 15.65
Convert Double Wrapper Object To Double Primitive Type
package com.jaladhi.jst;public class DoublePrimitive {
public static void main(String[] args) {
Double obj = new Double(20.65);
double num = obj.intValue();
System.out.println("Wrapper Object Value : " + obj);
System.out.println("Primitive Integer Value : " + num);
}
}
Primitive Double Value : 20.65
Convert Character Primitive Type to Character Wrapper Object
package com.jaladhi.jst;public class CharacterWrapperClass {
public static void main(String args[]) {
char c = 'b';
Character obj = Character.valueOf(c);
System.out.println("Primitive Character Number : " + c);
System.out.println("Wrapper Character Number : " + obj);
}
}
Converted Character Number : b
Convert Character Wrapper Object To Character Primitive Type
package com.jaladhi.jst;public class CharacterPrimitive {
public static void main(String[] args) {
Character obj = new Character('d');
char num = obj.charValue();
System.out.println("Wrapper Object Value : " + obj);
System.out.println("Primitive Character Value : " + num);
}
}
Primitive Character Value : d
Convert Byte Primitive Type to Byte Wrapper Object
package com.jaladhi.jst;public class ByteWrapperClass {
public static void main(String args[]) {
byte b = 5;
Byte obj = Byte.valueOf(b);
System.out.println("Primitive Byte Number : " + b);
System.out.println("Wrapper Byte Number : " + obj);
}
}
Converted Byte Number : 5
Convert Byte Wrapper Object To Byte Primitive Type
package com.jaladhi.jst;public class BytePrimitive {
public static void main(String[] args) {
Byte obj = new Byte("2");
byte num = obj.byteValue();
System.out.println("Wrapper Object Value : " + obj);
System.out.println("Primitive Byte Value : " + num);
}
}
Primitive Byte Value : 2
Convert Float Primitive Type to Float Wrapper Object
package com.jaladhi.jst;public class FloatWrapperClass {
public static void main(String args[]) {
float f = 60.8f;
Float obj = Float.valueOf(f);
System.out.println("Primitive Float Number : " + c);
System.out.println("Wrapper Float Number : " + obj);
}
}
Converted Float Number : 60.8
Convert Float Wrapper Object To Float Primitive Type
package com.jaladhi.jst;public class FloatPrimitive {
public static void main(String[] args) {
Float obj = new Float(49.5);
float num = obj.floatValue();
System.out.println("Wrapper Object Value : " + obj);
System.out.println("Primitive Float Value : " + num);
}
}
Primitive Float Value : 49.5
Convert Boolean Primitive Type to Boolean Wrapper Object
package com.jaladhi.jst;public class BooleanWrapperClass {
public static void main(String args[]) {
boolean b = true;
Boolean obj = Boolean.valueOf(b);
System.out.println("Primitive Boolean Number : " + b);
System.out.println("Wrapper Boolean Number : " + obj);
}
}
Converted Boolean Number : true
Convert Boolean Wrapper Object To Boolean Primitive Type
package com.jaladhi.jst;public class BooleanPrimitive {
public static void main(String[] args) {
Boolean obj = new Boolean(true);
boolean b = obj.booleanValue();
System.out.println("Wrapper Object Value : " + obj);
System.out.println("Primitive Boolean Value : " + b);
}
}
Primitive Boolean Value : true
Convert Long Primitive Type to Long Wrapper Object
package com.jaladhi.jst;public class BooleanWrapperClass {
public static void main(String args[]) {
long b = 1234;
Long obj = Long.valueOf(b);
System.out.println("Primitive Long Number : " + b);
System.out.println("Wrapper Long Number : " + obj);
}
}
Converted Long Number : 1234
Convert Long Wrapper Object To Long Primitive Type
package com.jaladhi.jst;public class LongPrimitive {
public static void main(String[] args) {
Long obj = new Long(9876);
long b = obj.longValue();
System.out.println("Wrapper Object Value : " + obj);
System.out.println("Primitive Long Value : " + b);
}
}
Primitive Long Value : 9876
Convert Short Primitive Type to Short Wrapper Object
package com.jaladhi.jst;public class ShortWrapperClass {
public static void main(String args[]) {
short b = 12;
Short obj = Short.valueOf(b);
System.out.println("Primitive Short Number : " + b);
System.out.println("Wrapper Short Number : " + obj);
}
}
Converted Short Number : 12
Convert Short Wrapper Object To Short Primitive Type
package com.jaladhi.jst;public class ShortPrimitive {
public static void main(String[] args) {
Short obj = new Short("12");
short b = obj.shortValue();
System.out.println("Wrapper Object Value : " + obj);
System.out.println("Primitive Short Value : " + b);
}
}
Primitive Short Value : 12