wellnwill Marathahalli Bangalore

Wrapper Classes

Wrapper classes are used to convert primitive type into object type. For every primitive type, there is a wrapper class. All wrapper classes are available in java.lang package.

Following are Wrapper classes available in java.

Primitive Data Type Wrapper class
boolean Boolean
byte Byte
char Character
short Short
int Integer
long Long
float Float
double Double
void Void
Parameter Constructors for Wrapper class Wrapper class
boolean or String Boolean
byte or String Byte
char Character
short or String Short
int or String Integer
long or String Long
float or String or double Float
double or String Double
Ex- Integer i1=new Integer(1);// primitive is converted into object type. Integer i2=new Integer(“1”);

If string argument is not representing a number then we will get runtime exception i.e. NumberFormatException.

Ex- Short s2=new Short(“one”);

In wrapper classes

toString() is overridden to return content/data. equals() is overridden for content/data comparison.

Utility methods

  • valueOf()
  • XXXValue()
  • parseXXX()
  • toString()

valueOf() method

This method is used to convert primitive/string to object type.

class Test
public static void main(String[] args)
// converting String/primitive to wrapper object by using constructor
Integer i1=new Integer(20);
Float f1=new Float(30);
// converting String/primitive to the wrapper object by using valueOf() 
Boolean b2=Boolean.valueOf("true");
Boolean b3=Boolean.valueOf("Raj");
Boolean b4=Boolean.valueOf("True");
Integer i4=Integer.valueOf(10);

XXXValue() method

This method is used to convert wrapper object to corresponding primitive type.

class Test1
public static void main(String[] args)
Integer i=Integer.valueOf(129);
System.out.println("byte value :"+i.byteValue());//-127
System.out.println("short value :"+i.shortValue());//129
System.out.println("int value :"+i.intValue());//129
System.out.println("long value :"+i.longValue());//129
System.out.println("float value :"+i.floatValue());//129.0

System.out.println("double value :"+i.doubleValue());//129.0
Character c1=new Character('s');
char ch=c1.charValue();
Boolean b1=new Boolean(false);
boolean b2=b1.booleanValue();

parseXXX() method

This method is used to convert String into the corresponding primitive.

When the data entered by user is in string type (i.e. through text field) and it represents a number and after that if we have to perform some mathematical operations then it is required to convert data from string type to integer type.

class Test3
public static void main(String[] args)
String st1="1";
String st2="2";
int i=Integer.parseInt(st1);
float f=Float.parseFloat(st2);

toString() method

We can use tostring() method to convert wrapper object or primitive to String.

if we get data from browser basically it is a form through that user enters data.

Generally, we get data in the form of String and if this string represents a number and any arithmetic operations are required then we convert this string to number format with the use of Integer.parseInt ().

Now again we have to show the modified data to the user on browser (i.e. form), we have to reconvert this data from number to string type with the use of toString ().

class Test4
public static void main(String[] args)
Integer i=new Integer(1);
String st1=i.toString();
Float f=new Float(1.1);
String st2=f.toString();


Process of converting primitive type into an object type is known as Boxing.


Process of converting an object type into primitive type is known as unboxing.

From java 1.5, boxing and unboxing process are implicitly supported by java and known as auto boxing and auto unboxing.

Void is the class representation of void keyword in java.

class Test5
public static void main(String[] args)
Integer i=new Integer(1); //Boxing
int x=i.intVaule();               //unboxing

class Test6
public static void main(String[] args)
Integer i=1;                                //auto boxing

int j=i;                                        //auto unboxing
class Test7
public static void main(String[] args)
Object O1[]=new Object[4];
O1[1]= new Test7();
O1[2]=1; //auto boxing, auto upcasting

Footer with Map