Wrapper

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

* Wrapper Classes : There is a Wrapper Class for every Primitive Data type in Java.

eg: Integer for int Float for float Object | | Number ( Abstract class) | -------------------------------------------------------------------------------| | | | | | | | Byte Short Integer Long Float Double Characte r Boolean (All wrapper classes are final) * Main functions: - To wrap primitives so that they can be handled like objects - To provide utility methods for primitives (usually conversions) Primitive Type ----------> <---------Wrapper Object

**** Important Utility Methods **** * Conversion of Primitive to the Wrapper Class int x=100; Integer i1=new Integer(x); Or Integer i2=x; //Autoboxing char ch= p ; Character c1=new Character(ch); Or Character c2=ch; //Autoboxing boolean b=true; Boolean b1=new Boolean(b); Or Boolean b2=b; //Autoboxing * Conversion of Wrapper Class to the Primitive (using xxxValue() method) Integer i1=new Integer(100); int x=i1.intValue(); Or int y=i1; //Autounboxing Character c1=new Character('p'); char x=c1.charValue(); Or char y=c1; //Autounboxing

Boolean b1=new Boolean(false); boolean x=b1.booleanValue(); Or boolean y=b1; //Autounboxing * Conversion of one Primitive to another Primitive (using xxxValue() method) (except boolean & char) int a=100; Integer i1=new Integer(a); byte b=i1.byteValue(); short s=i1.shortValue(); long l=i1.longValue(); float f=i1.floatValue(); double d=i1.doubleValue(); double a=100.3; Double i1=new Double(a); byte b=i1.byteValue(); short s=i1.shortValue(); long l=i1.longValue(); float f=i1.floatValue(); double d=i1.doubleValue(); * Conversion of String to the Primitive int i1=Integer.parseInt( 30 ); double d1=Double.parseDouble( 30.8 ); boolean b1=Boolean.parseBoolean( false ); throws NumberFormatException if parsing fails * Conversion of String to Wrapper Integer i1=Integer.valueOf( 30 ); Double d1=Double.valueOf( 30.8 ); Boolean b1=Boolean.valueOf( false ); * Conversion of Primitive to String int a=100; String s1=Integer.toString(a); char ch='p'; String s1=Character.toString(ch); boolean b=false; String s1=Boolean.toString(b); * Conversion of Wrapper to String Integer i1=new Integer(100); String s1=i1.toString(); Character c1=new Character('p'); String s1=c1.toString(); (using toString() method) (using static toString() method) (using static valueOf() method) (using static parseXxx() method)

Boolean b1=new Boolean(false); String s1=b1.toString(); * Summary primitive xxxValue() primitive parseXxx(String) Wrapper valueOf(String) - to convert a Wrapper to a primitive - to convert a String to a primitive - to convert a String to a Wrapper

* In case of Short & Integer if value is in the range -128 to +128 then single o bject is created * When == is used to compare a wrapper and wrapper(same types) : then references are compared wrapper and wrapper(different types) : not allowed compiler error incomp atible types primitive and wrapper : the wrapper will be unboxed & comparison will be primitive to primitive * When we use relational operators(<,>..)then unboxing happens * You cannot assign one wrapper to another for eg : Integer i1=100; Double d=i1; Double d=100.5; Integer i1=d; //not allowed //not allowed

int x=100; Integer y=x; //allowed (here autoboxing happens) int x=100; Long y=x; //not allowed Long y=(long)x; //allowed int x=100; Short y=x; //not allowed Short y=(short)x; //allowed Integer i1=new Integer(100); int x=i1; //allowed float y=i1; //allowed short z=i1; //not allowed

You might also like