wrapper classes

780 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

wrapper classes

  1. 1. Wrapper Classes
  2. 2. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 2 Problem with Primitives  Java is an object-oriented language. Means, everything in Java is an object  But, how about primitives?  Primitives cannot participate in the object activities, such as  Returned from a method as an object  Adding to a Collection of objects  As a solution to this problem, Java allows you to include the primitives in the family of objects by using Wrapper classes
  3. 3. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 3 Creating Wrapper Classes  For to each primitive data type in Java there is corresponding wrapper class  This class encapsulates a single value for the primitive data type  The wrapper object of a wrapper class can be created in one of two ways:  By instantiating the wrapper class with the new operator  By invoking a static method on the wrapper class
  4. 4. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 4 Primitive Types and Wrapper Classes
  5. 5. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 5 Creating Wrapper Classes with new Operator  Obviously, you can always pass the corresponding primitive data type as an argument to a wrapper class constructor  You can also pass a String as an argument to any wrapper class constructor except Character  The Character constructor only takes the obvious argument: char  You can pass double as an argument to the Float constructor but not vice versa  All the wrapper classes except Boolean and Character are subclasses of an abstract class called Number  Boolean and Character are derived directly from the Object class
  6. 6. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 6 Another way of Creating Wrappers  The wrapped value in a wrapper class cannot be modified  To wrap another value, you need to create another object  Wrappers are immutable  There will be situations in your program when you really don’t need a new instance of the wrapper class  but you still want to wrap a primitive  In this case, use the static method valueOf()
  7. 7. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 7 Using Static Method valueOf()
  8. 8. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 8  The valueOf() method in the Character class accepts only char as an argument  Any other wrapper class will accept either the corresponding primitive type or String as an argument  The valueOf() method in the integer number wrapper classes (Byte, Short, Integer, and Long) also accepts two arguments together:  A String  A radix, where radix is the base  For example, a decimal is radix 10 and a binary is radix 2 Using Static Method valueOf()
  9. 9. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 9  A storage capability without the retrieval capability is not of much use  Once we store a primitive in a wrapper object, often you will want to retrieve the stored primitive at a later time  All the number wrapper classes (Byte, Short, Integer, Long, Float, and Double) have the which can retrieve byte, short, int, long, float, or double  This is because all of these classes are subclasses of the Number class  It means you can store one primitive type in a wrapper and retrieve another one  So you can use the wrappers as a conversion machine Extracting Wrapped Values
  10. 10. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 10 Methods to extract values
  11. 11. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 11  We can use Wrappers for converting the type without creating the Wrapper  There is an appropriate static method in every wrapper class  For example, all the wrapper classes except Character offer a static method that has the following signature: static <type> parse<Type>(String s)  Ex: static int parseInt (String s)  Each of these methods parses the string passed in as a parameter and returns the corresponding primitive type Instant use of Wrapper Classes
  12. 12. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 12 Methods to Convert Strings to Primitives
  13. 13. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 13  Wrapper classes are used for two main purposes  To store a primitive type in an object so that it can participate in object-like operations  To convert one primitive type into another  However, as we have seen, you do this conversion between primitives and objects manually  If Java is a truly object-oriented language, why do we have to manually wrap the primitives into objects  why is the process not made automated? Well, that is exactly what autoboxing offers Wrapper Classes
  14. 14. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 14  Boxing and unboxing make using wrapper classes more convenient  In the old, pre-Java 5 days, if you wanted to make a wrapper, unwrap it, use it, and then rewrap it, you might do something like this: Integer y = new Integer(567); // make it int x = y.intValue(); // unwrap it x++; // use it y = new Integer(x); // re-wrap it System.out.println("y = " + i); // print it  Now, with new and improved Java 5 you can say Integer y = new Integer(567); // make it y++; // unwrap it, increment it, // rewrap it System.out.println("y = " + i); // print it Boxing and Unboxing
  15. 15. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 15  The Intention of the equals() method is to determine whether two instances of a given class are "meaningfully equivalent"  This definition is intentionally subjective  it's up to the creator of the class to determine what "equivalent" means for objects of the class  For all wrapper classes, two objects are equal if they are of the same type and have the same value Boxing, == and .equals()
  16. 16. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 16  Widening, Var-Args and Auto-Boxing make method overloadig a little tricky  When a class has overloaded methods, the compiler's jobs is to determine which method to use whenever it finds an invocation for the overloaded method Auto-Boxing and Overloading
  17. 17. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 17 class AddBoxing { static void go(Integer x) { System.out.println("Integer"); } static void go(long x) { System.out.println("long"); } public static void main(String [] args) { int i = 5; go(i); // which go() will be invoked? } } Overloading with Boxing and Widening
  18. 18. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 18 class AddVarargs { static void go(int x, int y) { System.out.println("int,int"); } static void go(byte... x) { System.out.println("byte... "); } public static void main(String[] args) { byte b = 5; go(b,b); // which go() will be invoked? } } Overloading with Var-Args and Widening
  19. 19. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 19  Even though each invocation will require some sort of conversion, the compiler will choose the older style before it chooses the newer style  This keeps the existing code more robust.  So far we've seen that  Widening beats boxing  Widening beats var-args  Does does boxing beat var-args? Reason
  20. 20. 10/02/13 RENAISSANCE SOFTLABS (P) LTD. 20 class BoxOrVararg { static void go(Byte x, Byte y) { System.out.println("Byte, Byte"); } static void go(byte... x) { System.out.println("byte... "); } public static void main(String [] args) { byte b = 5; go(b,b); // which go() will be invoked? } } Overloading with Var-Args and Boxing

×