Java Wrapper Classes
Handled By
Dr.T.Abirami
Associate Professor
Department of IT
Kongu Engineering College Perundurai
Wrapper classes
• The wrapper classes in Java are used to
convert primitive types (int, char, float, etc)
into corresponding objects.
Need for Wrapper Classes
• Generic classes only work with objects and
don't support primitives.
• Data structures in the Collection framework such
as ArrayList and Vector store only the objects
(reference types) and not the primitive types.
• The object is needed to
support synchronization in multithreading.
Importance of Wrapper classes
There are mainly two uses with wrapper classes.
1) To convert simple data types into objects,
that is, to give object form to a data type; here
constructors are used.
2) To convert strings into data types (known as
parsing operations), here methods of type
parseYYY () are used. YYY->datatypes
Features of the Java wrapper Classes.
1) Wrapper classes convert numeric strings into
numeric values.
2) The way to store primitive data in an object.
3) The valueOf() method is available in all
wrapper classes except Character
4) All wrapper classes have datatypeValue()
method. This method returns the value of the
object as its primitive type.
Wrapper Classes
• To convert primitive data type into object and
object into primitive.
• The automatic conversion of primitive into an
object is known as autoboxing
• objects into primitives automatically is called
unboxing
• The eight classes of the java.lang package are
known as wrapper classes in Java.
• The list of eight wrapper classes are given
below:
Wrapper Classes
Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Creating Wrapper Objects
public class MyClass {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}
• valueOf() method : To create Wrapper object
for given primitive data type or String.
• parseInt() ,parseFloat(), parseDouble()
method : to convert String class (Wrapper
class) to primitive
methods of Wrapper classes
methods of Wrapper classes
Method Purpose
parseInt(s) returns a signed decimal integer value equivalent to string s
toString(i) returns a new String object representing the integer i
byteValue() returns the value of this Integer as a byte
doubleValue() returns the value of this Integer as a double
floatValue() returns the value of this Integer as a float
intValue() returns the value of this Integer as an int
shortValue() returns the value of this Integer as a short
longValue() returns the value of this Integer as a long
Creating Wrapper Objects
public class MyClass {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}
Convert Wrapper class into Primitive
data types
public class MyClass {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt.intValue());
System.out.println(myDouble.doubleValue());
System.out.println(myChar.charValue());
}
}
autoboxing
public class AutoBoxingTest
{
public static void main(String args[])
{
int num = 10; // int primitive
Integer obj = Integer.valueOf(num);
// creating a wrapper class object
System.out.println(num + " " + obj);
}
}
Unboxing
public class UnboxingTest
{
public static void main(String args[]) {
Integer obj = new Integer(10);
// Creating Wrapper class object
int num = obj.intValue();
// Converting the wrapper object to primitive datatype
System.out.println(num + " " + obj);
}
}
Primitive data type to Wrapper class
//Autoboxing example of int to Integer
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);
}}
Wrapper class to Primitive data type
//Unboxing example of Integer to int
class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();
//converting Integer to int explicitly
System.out.println(a+" "+i);
}}
toString()
// Java program to illustrate toString()
class GFG {
public static void main(String[] args)
{
Integer I = new Integer(10);
String s = I.toString();
System.out.println(s);
}
}

Wrapper classes

  • 1.
    Java Wrapper Classes HandledBy Dr.T.Abirami Associate Professor Department of IT Kongu Engineering College Perundurai
  • 2.
    Wrapper classes • Thewrapper classes in Java are used to convert primitive types (int, char, float, etc) into corresponding objects.
  • 3.
    Need for WrapperClasses • Generic classes only work with objects and don't support primitives. • Data structures in the Collection framework such as ArrayList and Vector store only the objects (reference types) and not the primitive types. • The object is needed to support synchronization in multithreading.
  • 4.
    Importance of Wrapperclasses There are mainly two uses with wrapper classes. 1) To convert simple data types into objects, that is, to give object form to a data type; here constructors are used. 2) To convert strings into data types (known as parsing operations), here methods of type parseYYY () are used. YYY->datatypes
  • 5.
    Features of theJava wrapper Classes. 1) Wrapper classes convert numeric strings into numeric values. 2) The way to store primitive data in an object. 3) The valueOf() method is available in all wrapper classes except Character 4) All wrapper classes have datatypeValue() method. This method returns the value of the object as its primitive type.
  • 6.
    Wrapper Classes • Toconvert primitive data type into object and object into primitive. • The automatic conversion of primitive into an object is known as autoboxing • objects into primitives automatically is called unboxing
  • 7.
    • The eightclasses of the java.lang package are known as wrapper classes in Java. • The list of eight wrapper classes are given below: Wrapper Classes Primitive Type Wrapper class boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double
  • 8.
    Creating Wrapper Objects publicclass MyClass { public static void main(String[] args) { Integer myInt = 5; Double myDouble = 5.99; Character myChar = 'A'; System.out.println(myInt); System.out.println(myDouble); System.out.println(myChar); } }
  • 9.
    • valueOf() method: To create Wrapper object for given primitive data type or String. • parseInt() ,parseFloat(), parseDouble() method : to convert String class (Wrapper class) to primitive methods of Wrapper classes
  • 10.
    methods of Wrapperclasses Method Purpose parseInt(s) returns a signed decimal integer value equivalent to string s toString(i) returns a new String object representing the integer i byteValue() returns the value of this Integer as a byte doubleValue() returns the value of this Integer as a double floatValue() returns the value of this Integer as a float intValue() returns the value of this Integer as an int shortValue() returns the value of this Integer as a short longValue() returns the value of this Integer as a long
  • 11.
    Creating Wrapper Objects publicclass MyClass { public static void main(String[] args) { Integer myInt = 5; Double myDouble = 5.99; Character myChar = 'A'; System.out.println(myInt); System.out.println(myDouble); System.out.println(myChar); } }
  • 12.
    Convert Wrapper classinto Primitive data types public class MyClass { public static void main(String[] args) { Integer myInt = 5; Double myDouble = 5.99; Character myChar = 'A'; System.out.println(myInt.intValue()); System.out.println(myDouble.doubleValue()); System.out.println(myChar.charValue()); } }
  • 13.
    autoboxing public class AutoBoxingTest { publicstatic void main(String args[]) { int num = 10; // int primitive Integer obj = Integer.valueOf(num); // creating a wrapper class object System.out.println(num + " " + obj); } }
  • 14.
    Unboxing public class UnboxingTest { publicstatic void main(String args[]) { Integer obj = new Integer(10); // Creating Wrapper class object int num = obj.intValue(); // Converting the wrapper object to primitive datatype System.out.println(num + " " + obj); } }
  • 15.
    Primitive data typeto Wrapper class //Autoboxing example of int to Integer 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); }}
  • 16.
    Wrapper class toPrimitive data type //Unboxing example of Integer to int class WrapperExample2{ public static void main(String args[]){ //Converting Integer to int Integer a=new Integer(3); int i=a.intValue(); //converting Integer to int explicitly System.out.println(a+" "+i); }}
  • 17.
    toString() // Java programto illustrate toString() class GFG { public static void main(String[] args) { Integer I = new Integer(10); String s = I.toString(); System.out.println(s); } }