0% found this document useful (0 votes)
32 views10 pages

Wrapper Classes Wrapper Classes

Wrapper classes wrap primitive types like int and double into object form. They define constructors that allow creation from primitives or strings. Utility methods include valueOf() to create wrapper objects, xxxValue() to convert to primitives, parseXxx() to parse strings, and toString() to convert to strings. The main objectives are to handle primitives like objects and define useful functions for them.

Uploaded by

mail.sushilk8403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views10 pages

Wrapper Classes Wrapper Classes

Wrapper classes wrap primitive types like int and double into object form. They define constructors that allow creation from primitives or strings. Utility methods include valueOf() to create wrapper objects, xxxValue() to convert to primitives, parseXxx() to parse strings, and toString() to convert to strings. The main objectives are to handle primitives like objects and define useful functions for them.

Uploaded by

mail.sushilk8403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Wrapper classes

Wrapper classes
The main objectives of wrapper classes are to wrap primitives into object form so that we can handle
primitives also just like objects. To define several utility functions which are required for the primitives.

Constructors
All most all wrapper classes define the following 2 constructors one can take corresponding primitive as
argument and the other can take String as argument.
Example
Integer i=new lnteger(lO);
Integer i=new lnteger("lO");
If the String is not properly formatted then we will get runtime exception saying
"NumberformatException".

 Float class defines 3 constructors with float, String and double arguments.
Float f=new Float (10.Sf);
Float f=new Float ("10.Sf');
Float f=new Float(l0.5);
Float f=new Float ("10.5");

 Character class defines only one constructor which can take char primitive as argument there is no
String argument constructor.
Character ch=new Character('a'); //valld
Character ch=new Character("a"); //invalid

 Boolean class defines 2 constructors with boolean primitive and String arguments. If we want to
pass boolean primitive the only allowed values are true, false where case should be lower case.

class WrapperClassDemo
{
public static void main(String[] args)throws Exception
{
Boolean bl=new Boolean("true11 );
Boolean b2=new Boolean("True");
Boolean b3=new Boolean("false");
Boolean b4=new Boolean("False");
Boolean bS=new Boolean("bhaskar");
System .o ut.printl n( bl) ;//true
System.out.println(b2);//true
System.out.println(b3);//false
System .out.println(b4);//false
System.out.println(bS);//false

}
}
Note:
1. In all wrapper classes toString() method is overridden to return its content.
2. In all wrapper classes .equals() method is overridden for content compression.
Utility methods
 valueOf() method.
 XXXValue() method.
 parseXxx() method.
 toString() method.

valueOf() method
We can use valueOf() method to create wrapper object for the given primitive or String this method is
alternative to constructor.Every wrapper class except Character class contains a static valueOf() method to
create wrapper object for the given String.
public static wrapper valueOf (String s);

Example
class WrapperClassDemo
{
public static void main (String [] args) throws Exception
{
Integer i=lnteger.valueOf("l0");
Double d=Double.valueOf("l0.5");
Boolean b=Boolean.valueOf("bhaskar");
System.out.println(i); //10
System.out.println(d); //10.5
System.out.println(b); //false
}
}
Form 2
Every integral type wrapper class (Byte, Short, Integer, and Long) contains the following valueOf() method
to convert specified radix string to wrapper object.
public static Integer valueOf (String s, int radix)

Note
The allowed radix (base) range is 2-36.
base2 0 to 1
bases8 0 to 7
base10 0 to 9
base11 0 to 9, a
base16 0 to 9, a to f
base36 0 to 9, a to z

Example
class WrapperClassDemo
{
public static void main(String[] args)
{
Integer i=lnteger.valueOf("l00",2);
System.out.println(i); //4
}
}

Form 3
Every wrapper class including Character class defines valueOf() method to convert primitive to wrapper
object.
public static wrapper valueOf(primitive p);

Example
class WrapperClassDemo
{
public static void main(String[] args)throws Exception
{
Integer i=lnteger.valueOf(l0);
Double d=Double.valueOf(l0.5);
Boolean b=Boolean.valueOf(true);
System.out.println(i); //10
System.out.println( d); //10.5
System.out.println(b);//true
}
}

xxxValue() method
We can use xxxValue() methods to convert wrapper object to primitive.Every number type wrapper class
(Byte, Short, Integer, Long, Float, Double) contains the following sic xxxValue() methods to convert
wrapper object to primitives.
1. public byte byteValue()
2. public short shortValue()
3. public int intValue()
4. public long longValue()
5. public float floatValue()
6. public double doubleValue()

Example
class WrapperClassDemo
{
public static void main(String[] args)throws Exception
{
Integer i=new lnteger(130);
System.out.println(i.byteValue()); //-126
System.out.println(i.shortValue()); //130
System.out.println(i.intValue()); //130
System.out.println(i.longValue()); //130
System.out.println(i.floatValue()); //130.0
System.out. pri ntl n(i. dou bleValue()) ; //130.0
}
}

charValue() method
Character class contains charValue() method to convert Character object to char primitive.
public char charValue();
Example
class WrapperClassDemo
{
public static void main{String[] args)
{
Character ch=new Character('a');
char c=ch.charValue();
System.out.println(c); //a
}
}

booleanValue()method:
Boolean class contains booleanValue{) method to convert Boolean object to boolean primitive.
public boolean booleanValue();
Example
class WrapperClassDemo
{
public static void main{String[] args)
{
Boolean b=new Boolean("bhaskar");
boolean bl=b.booleanValue();
System.out.println(bl); //false
}
}

parseXxx() method
We can use this method to convert String to corresponding primitive.
Forml
Every wrapper class except Character class contains a static parseXxx() method to convert String to
corresponding primitive.
public static primitive parseXxx(String s);
Example
class WrapperClassDemo
{
public static void main{String[l args)
{
int i=lnteger.parselnt{"lO");
boolean b=Boolean.parseBoolean{"bhaskar");
double d=Double.parseDouble{"lO.S");
System.out.println(i); //10
System.out.println(b); //false
System.out.println(d); //10.5
}
}
Form 2
Integrat type wrapper classes(Byte, Short, Integer, Long) contains the following parseXxx() method to
convert specified radix String form to corresponding primitive.
public static primitive parseXxx(String s,int radix);
Example
class WrapperClassDemo
{
public static void main(String[) args)
{
int i=lnteger.parselnt("l00",2);
System.out.println(i); //4
}
}
toString() method
We can use toString() method to convert wrapper object (or) primitive to String.
Form 1
public String toString();
 Every wrapper class including Character class contains the above toString() method to convert
wrapper object to String.
 It is the overriding version of Object class toString() method.
 Whenever we are trying to print wrapper object reference internally this toString() method only
executed.
Example
class WrapperClassDemo
{
public static void main{String[] args)
{
Integer i=lnteger. valueOf("l0");
System.out.println(i); //10
System.out.pri ntln{i. toString() ); //10
}
}

Form 2
Every wrapper class contains a static toString() method to convert primitive to String.
public static String toString(primitive p);

Example
class WrapperClassDemo
{
public static void main(String[] args)
{
String sl=lnteger.toString(lO);
String s2=Boolean.toString(true);
System.out.println(sl); //10
System.out.println(s2); //true
}
}

Form 3
Integer and long classes contains the following static toString() method to convert the primitive to specified
radix String form.
public static String toString(primitive p,int radix);
Example
class WrapperClassDemo
{
public static void main(String[) args)
{
String sl=lnteger.toString(7,2);
String s2=1nteger.toString(l 7,2);
System.out.println(sl);//111
System.out.println(s2);//10001
}
}
Form 4
Integer and Long classes contains the following toXxxString() methods.
public static String toBinaryString(primitive p);
public static String toOctalString(primitive p);
public static String toHexString(primitive p);
Example
class WrapperClassDemo
{
public static void main(String[] args)
{
String sl=lnteger.toBinaryString(7);
String s2=1nteger.toOcta1String(10);
String s3=1nteger.toHexString(20);
System .out.println(sl); //111
System.out.println(s2); //12
System.out.println(s3); //14
}
}

Autoboxing and Auto unboxing


Until 1.4 version we can't provide wrapper object in the place of primitive and primitive in the place of
wrapper object all the required conversions should be performed explicitly by the programmer.
Example
class AutoBoxingAndUnboxingDemo
{
public static void main(String[] args)
{
Boolean b=new Boolean(true);
if(b)
{
System.out.println("hello");
}
}
}

Example
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main(String[J args)
{
ArrayList l=new ArrayList();
l.add(lO);
}
}

But from 1.5 version onwards we can provide primitive value in the place of wrapper and wrapper object in
the place of primitive all required conversions will be performed automatically by compiler. These
automatic conversions are called Autoboxing and Autounboxing.
Example
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main(String[] args)
{
Arraylist l=new Arraylist();
Integer i=new lnteger(lO);
l.add(i);
}
}

Autoboxing
Automatic conversion of primitive to wrapper object by compiler is calledAutoboxing.
Example
Integer i=l0; [compiler converts "int'' to "Integer" automatically by Autoboxing]
• After compilation the above line will become.
Integer i=lnteger.valueOf(l0);
• That is internally Autoboxing concept is implemented by using valueOf() method.

Autounboxing
Automatic conversion of wrapper object to primitive by compiler is called Autounboxing.
Example
Integer i=new lnteger(l0);
Int i=I; [compiler converts "Integer" to "int" automatically by Autounboxing]
• After compilation the above line will become.
Int i=l.intValue();
• That is Autounboxing concept is internally implemented by using xxxValue() method.

Note: From 1.5 version onwards we can use primitives and wrapper objects interchangly the required
conversions will be performed automatically by compiler.

Example:
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
static Integer l=O;
public static void main(String[] args)
{
int i=I;
System.out.println(i); //0
}

Example:
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main(String[] args)
{
Integer x=lO;
Integer y=x;
++x;
System.out.println(x); //11
System.out.println(y);//10
System.out.print! n(x==y) ; //false
}
}

All wrapper objects are immutable that is once we created a wrapper object we can't perform any changes
in the existing object. If we are trying to perform any changes with those changes a new object wilt be
created.

Example:
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main(String[] args)
{
Integer x=new lnteger(lO);
Integer y=new lnteger(lO);
System.out.println(x==y); //false
}
}

Example:
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main(String[] args)
{
Integer x=new lnteger(10);
Integer y=10;
System.out.println(x==y); //false
}
}

Example:
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main{String[] args)
{
Integer x=new lnteger(10);
Integer y=x;
System.out.println{x==y); //true
}
}

Example:
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main{String[] args)
{
Integer x=10;
Integer y=10;
System.out.println{x==y); //true

}
}

Example:
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main{String[] args)
{
Integer x=lOO;
Integer y=lOO;
System.out.println{x==y); //true
}
}

Example:
import java.util. *;
class AutoBoxingAndUnboxingDemo
{
public static void main(String[] args)
{
Integer x=l000;
Integer y=l000;
System.out.pri ntln(x==y);//false
}
}

Key points:
 To implement the Autoboxing concept in every wrapper class a buffer of objects will be created at
the time of class loading.
 By Autoboxing if an object is required to create 1st JVM will check whether that object is available
in the buffer or not. If it is available then JVM will reuse that buffered object instead of creating
new object. If the object is not available in the buffer then only a new object will be created. This
approach improves performance and memory utilization.
 But this buffer concept is available only in the following cases.

Byte Always
Short -128 To 127
Integer -128 To 127
long -128 To 127
Character 0To 127
Boolean Always
 In all the remaining cases compulsory a new object will be created.

You might also like