Wednesday, November 11, 2015

Wrapper class with Example in Java

Wrapper class with Example in Java

The Main Objective of Wrapper class are:
       
To Wrap primitives into object form. So that we can handle primitives also just like objects.
To Define several utility functions for the primitives(like converting primitive to the string form etc.) Wrapper classes and all the methods that manipulate data and allows to operate a certain work. 

Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object   into primitive automatically. The automatic conversion of primitive into object is known and autoboxing and vice-versa unboxing.Wrapper class is a wrapper around a primitive data type. All of the primitive wrapper classes in Java are immutable i.e. once assigned a value to a wrapper class instance cannot be changed further.Wrapper Classes are used broadly with Collection classes in the java.util package and with the classes in the java.lang.reflect reflection package.

These are the Constructors in Wrapper class:

Constructors:
 valueOf()
 xxxValue()
 parseXxx()
 toString()

Constructing Wrapper objects by using constructors:
   
Every Wrapper class contains 2 constructors one can take the corresponding primitive as the argument and the other can take the corresponding string as the argument.

Example:

Integer I = new Integer(10);
Integer I = new Integer(“10”);
          
 If the String is unable to convert into the number form then we will get runtime exception saying “NumberformatException”.

Ex: 

Integer I = new Integer(“ten”);

Float class contains 2 constructors which can take double String as argument.

Float f = new Float(10.5f);
Float f = new Float(“10.5f”);
Float f = new Float(10.5);
Float f = new Float(10.5);

Character class contain only one constructor which can take char as the argument i.e character class doesn’t contain a constructor which can take string as the argument.
         
Ex:

Character ch1 = new Character('a'); // valid.
Character ch1 = new Character("a"); // not valid.
         
Boolean class contains 2 constructors one can take boolean primitive. Other can take string
argument. If u r providing boolean primitive as the argument the. The allowed values are true or  false.

Case is not important if the content contains ‘TRUE’ then it is considered as true other wise it considered as false.
 
Following table lists the primitive types and the corresponding wrapper classes:
                
                          Primitive                   Wrapper
                         
                         boolean                     java.lang.Boolean
                             byte                       java.lang.Byte
                            char                     java.lang.Character
                        double                       java.lang.Double
                           float                        java.lang.Float
                             int                       java.lang.Integer
                             long                       java.lang.Long
                            short                      java.lang.Short
                           void                       java.lang.Void


In Java 5.0 version, additional wrapper classes were introduced in the java.util.concurrent.atomicpackage.They provide atomic operations for assignment, addition and increment

                     Primitive                           Wrapper
                        boolean                         AtomicBoolean
                             int                            AtomicInteger
                            long                          AtomicLong
                              V                            AtomicReference<V>


 Features Of the Wrapper Classes

Some of the sound features maintained by the Wrapper Classes are as under :

1)    All the methods of the wrapper classes are static.
2)   The Wrapper class does not contain constructors.
3)   Once a value is assigned to a wrapper class instance it can not be changed, anymore.

valueOf( ) methods: 
version1:

All the wrapper classes except character class contains the valueOf() method for convertingstring to corresponding Wrapper Object.

   public static wrapper valueOf(String s);


Ex:

Integer I = Integer.valueOf(‘10’);
Float F = Float.valueOf(“10.5”);
Boolean B = Boolean.valueOf(“raju”);
Character ch = Character.valueOf(“10”); // CompiletimeError

Version2:
       
All Integral wrapper classes “Byte, Short, Long, Integer” Contains the following valueOf()  method.

public static wrapper valueOf(String s, int radix);

The allowed base of radix is 1 to 36.Because Numerics(10) ,Alphabets(26) finally 10+26 =36

 Ex:

Integer I = Integer.valueOf(“101011”, 2);
System.out.println(I);
O/P:- 43

Version3:
             
Every Wrapper class including character class contains the following valueOf() method to convert primitive to wrapper object form.
                 
public static wrapper valueOf(primitive p);


Ex:

Integer I = Integer.valueOf(10);
Character ch = Character.valueOf(‘a’);
Boolean B = Boolean.valueOf(true);

               
Version1, Version2 are  String to wrapper object.

 Version3 is  primitive to wrapper object.

xxxValue() method:
Every wrapper class Except character and Boolean classes contains the following xxxValue() methds

for converting wrapperObject to primitive:

public int intValue();
public byte byteValue();
public short shortValue();
public long longValue();
public float floatValue();
public int doubleValue();

Ex:
                            
Integer I = Integer.valueOf(130);
System.out.println(I.byteValue());
System.out.println(I.shortValue()); //-126
System.out.println(I.intValue()); //130
System.out.println(I.langValue());//130
System.out.println(I.floatValue()); //130.0
System.out.println(I.doubleValue()); //130.0

Character class contain charValue() method to return char primitive for the given character wrapper object.
                
public char charValue();

Ex
character ch = new character(‘a’);
char ch = ch.charValue();
System.out.println(ch1);
O/P:- a


        
Boolean class contains booleanValue() method to return boolean primitive for the given boolean objective.
 Boolean B = Boolean.valueOf("Tea Break");
  boolean b1 = B.booleanValue();
  System.out.println(b1); 

O/P:-false

Note:- In total 38 xxxValue methods are possible ((6 X 6) +1 + 1) = 38

parseXxx() methods:

version1:

Every wrapper class except character class contains the following parseXxx() method for converting String to primitive type

public static primitive parseXxx(String s);
         
Ex:

int i= Integer.valueOf(“10”);
Boolean b = Boolean.parseBoolean(“true”);

Version2:

Integral wrapper classes(Byte, Short, Integer and long ) contains the following parseXxx()method.

public static primitive parseXxx(String s, int radix );

int i = Integer.parseInt(“101011”, 2);
System.out.println(i); 
                  
 O/P:-43

toSting()methods:

version1:

All wrapper Classes contains an instance method toString() for converting wrapper object to
String type. This is overriding method of object class toString() method.

public String toString();
Integer I = new Integer(10);
String s = I.toString();
System.out.println(s);

O/P:-10


 Boolean B = new Boolean(“raju”);
 String s = B.toString();
 System.out.println(s); 

 O/P:-false


Version2:

Every wrapper class contains the following static toString() for converting primitive to String representation..

 public static String toString(primitive p);

String s = Integer.toString(10);
System.out.println(s); 

O/P:-10

String s = Boolean.toString(true);
System.out.println(s);
O/P:-true

Version3:

Integer and long classes contains the following toString() to return the String in the specified radix.

public static String toString(int/long, int radix );

String s = Integer.toString(43, 2);
System.out.println(s); 
O/P:-“101011”

String s = Integer.toString(43, 8);
System.out.println(s); 
O/P:-“53”


Version4:

Integer and Long classes contains the following methods to return specified radix String
form.

public static String toBinaryString(int/long, l);
public static String toOctalString(int/long, l);
public static String toHexString(int/long, l);
String s = Integer.toBinaryString(43);
System.out.println(s);

O/P:-“101011”

String s = Integer.toOctalString(43);
System.out.println(s);

 O/P:-“53”
String s = Integer.toHexString(43);
System.out.println(s);

 O/P:-“262”

                  

2 comments:

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...