Sunday, February 15, 2009

Wrapper Class

The Wrapper classes have introduced for the fallowing two purposes by SUN people.
1. To wrap primitives into object form so that we can handle primitives also just like objects.
2. We can get several utility functions for primitives.
• The fallowing are the wrapper classes.
Type --------- Class
byte ---------> Byte
short ---------> Short
int ---------> Integer
long ----------> Long
float ----------> Float
double ----------> Double
char ----------> Character
boolean ----------> Boolean



Constructors of the Wrapper classes :
• Integer class contains the fallowing two constructors.
Integer i= new Integer(int i);
Integer i= new Integer(String s);
Ex: Integer i= new Integer(10);
Integer i= new Integer(“10”);
Integer i=new Integer(“ten”); x given
RTE: NumberFormatException
I.e., If the String is unable to convert into a number then we will get a RTE saying “NumberFormantException”.
---> Byte b=new Byte(byte b);
---> Byte b=new Byte(String s);
Wrapper Class --------- Constructor Arguments
1. Byte --------------------> byte or String
2. Short --------------------> short or String
3. Integer ---------------------> int or String
4. Long ---------------------> long or String
5. Float ---------------------> float or String or double
Ex: float f= new Float(float f); //10.0f //valid
float f= new Float(String s); //”10.0f” //valid
float f= new Float(double d);//10.0 //valid
6. Double ----------------------> double
7. Character ------------------> char
Ex: Character c=new Character(char c);
Character c=new Character(‘d’); //valid
Character c=new Character(“d”); //invalid
8. Boolean ---------------------> Boolean or String
Ex: Boolean b=new Boolean(true); //valid
(false); //valid
(TRUE); //invalid
(FALSE); //invalid
Boolean b=new Boolean(“viswan”); //false
(“yes”); //false
(“no”); //false
Note: Other than “true” everything should return ‘false’.
Which of the fallowing are valid Boolean declarations?
1. boolean b=true; //valid
2. boolean b=false; //valid
3. boolean b=TRUE; //invalid
4. boolean b=FALSE; //invalid
5. Boolean b=new Boolean(“true”); //valid
6. Boolean b=new Boolean(“false”); //valid
7. Boolean b=new Boolean(“yes”); //valid //false
8. Boolean b=new Boolean(“NO”); //valid // false
9. boolean b=yes; //invalid //Compile time error
10. boolean b=NO; //invalid // Compiletime error
valueOf() method:


• Except character class every wrapper class contain a static valueOf() which can be used for converting a String object to corresponding wrapper object.
Ex: Integer i=Integer.valueOf(“10”); //valid
Integer i=Integer.valueOf(“ten”); //invalid // RTE :NumberFormatException
Boolean b= Boolen.valueOf(“xxx”); //false
valueOf(String):
String object ----------to------------> Wrapper Object
• This is a static method.
• The argument is String only.
• Throws NumberFormatException, if we are unable to convert the String to number.
Ex: Integer i= Integer.valueOf(10); //invalid
Argument is not String ,the Second version of valueOf() method:
• Byte. Short, Integer, Long classes contain second version of valueOf() method which can take a String and radix as arguments.
• First the String converted into decimal form and then that decimal value will be stored in the wrapper object.
Ex1: Integer i=Integer.valueOf(”101100”,2) ;
System.out.println(i);//44 base=radix
Signatures:
1. public static wrapper valueOf(String s)
object
2. public static wrapper valueOf(String s, int radix)
object
Ex2: Long l=Long.valueOf(“12”,5);
System.out.println(l); //7
* totally “36” bases or radixes are valid
xxxValue() methods:-
• All the numeric wrapper classes contains the fallowing methods for “converting wrapperclass object to primitive”.
intVlaue(); byteVlaue(); shortVlaue(); longVlaue(); floatVlaue(); doubleVlaue();
• Each wrapper class contains the above six methods, in total 36 xxxValue() methods are possible.
• These xxxValue() methods are instance methods and no arg methods.
Ex: Double d =new(150,263);
System.out.println(d.byteValue()); // -106
System.out.println(d.shortValue()); //150
System.out.println(d.intValue()); //150
System.out.println(d.longValue()); // 150
System.out.println(d.floatValue()); // 150.263
System.out.println(d.doubleValue()); // 150.263



• Character class contains charValue() method for converting character object to primitive.
Ex: Character ch= new Character(‘a’) ;
Char c=ch.charValue();
System.out.println(c); // a
• Boolean class contain booleaValue() method for converting a Boolean object to Boolean primitive.
Ex: Boolean B=new Boolen(“viswan”);
boolean b=B.charValue();
System.out.println(b); //false
parseXxx() methods:


• Every wrapper class except Character class contain parseXxx() for converting a String object to corresponding primitive.
Ex: String s=”10”;
int i=Integer.parseInt(s);
double d=Double.parseDouble(s); //10.0
long l=Long.parseLong(s);
Wrapperclass--------- parseXxx()
Byte ------------> parseByte(String s)
Short -------------> parseShort(String s)
Integer -------------> parseInt(String s)
Long -------------> parseLong(String s)
Float -----------> parseFloat(String s)
Double ------------> parseDouble(String s)
• Boolean class contain a static method getBoolean() method for converting a string to Boolean primitive.. -----> 1.4 version
String s=”123”;
int i=Integer.parseInt(s);
double d=Double.parseDouble(s);
System.out.println(i); //123
System.out.println(d); //123.0
Boolean b=getBoolean(“viswan”);
System.out.println(b); //false
Second version of parseXxx() :-
• All the integral wrapper classes (byte, short, integer, long) contains second version of parseXxx().
public static primitive parseXxx(String s,int radix)
Ex: int i=Integer.parseInt(“10101100”,2);
System.out.println(i); //172
long l=Long.parseLong(“12”,8);
System.out.println(l); //10
• java support max – radix is 36(base)
System.out.println(Character.MAX-RADIX); //036
toString() method:
1st version:-
• All the wrapper classes contains an instance toString() method for converting wrapper class object to corresponding String object.
• public String toString();
Integer i=new Integer(10);
String s= i.toString();
System.out.println(s); // 10
Boolen B=new Boolean(“surya”);
String s=B.toString();
System.out.println(s);//false


2nd Version:


• Every wrapper class contain a static toString() method for converting a primitive to String object.
• This is available in all wrapper classes and object class also includes Boolean and Character classes.
• public static String toString(10);
Ex: String s1=Integer.toString(10);
System.out.println(s);
String s1=Boolean.toString(true);
String s1=Integer.toString(‘a’);
System.out.println(s1);
System.out.println(s2);
3rd Version:


• Integer and Long classes contain the 3rd version of the toString() method for converting given primitive to String of sprcified radix.
• public satic String toString(30,2);
Ex: String s= Integer.toString(30,2);
System.out.println(s); //”11110”
toString(primitive, int radix)
4th version:
• public static String to xxxString(primitive) //int/long
• The version of toString() is available in the Integer and Long classes only. The possible to xxxString() methods are, toBinaryString, toHexString and tooctalString for converting the given primitive to the corresponding String form.
String s=Integr.toBinaryString(100); //1100100
String s=Integr.toOctalString(100); //144
String s=Integr.toHexString(100); //64
• All the wrapper class objects are immutable and all the wrapper classes are final classes.
• ‘void’ is also one type of wrapper class.
• String class and all the wrapper classes are immutable.
• The fallowing are the final classes.
-->String
--> StrringBuffer
--->Math

0 comments:

Post a Comment