Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Primitive Wrappers


Published on

  • Be the first to comment

  • Be the first to like this

Primitive Wrappers

  1. 1. Primitive Wrappers
  2. 2. Prime Benefits <ul><li>Provides a way of wrapping primitive values in an object so that a primitive can be treated as an object. </li></ul><ul><li>To provide a set of utility methods for primitives. </li></ul>
  3. 3. Situations when required <ul><li>When primitives are required to be added to a collection object. </li></ul><ul><li>When we want to return a primitive from a method that returns an object. </li></ul><ul><li>When converting primitives to and from String objects. </li></ul><ul><li>When converting primitives & String objects to and from different bases like binary, octal & hexadecimal. </li></ul>
  4. 4. Wrapper Constructors
  5. 5. Creating Objects <ul><li>Integer i1 = new Integer(5); </li></ul><ul><li>Integer i2 = new Integer(“5”); </li></ul><ul><li>Float f1 = new Float(5.6f); </li></ul><ul><li>Float f2 = new Float(“5.6f”); </li></ul><ul><li>Double d1 = new Double(6.7); </li></ul><ul><li>Double d2 = new Double(“6.7”); </li></ul><ul><li>Character c = new Character(‘a’); </li></ul><ul><li>Boolean b1 = new Boolean(true); </li></ul><ul><li>Boolean b2 = new Boolean(“true”); </li></ul><ul><li>Boolean b3 = new Boolean(“TrUe”); </li></ul>
  6. 6. Creating Objects contd. <ul><li>valueOf() methods </li></ul><ul><li>Integer i1 = Integer.valueOf(“101”,2); </li></ul><ul><li>Integer i2 = Integer.valueOf(“F”,16); </li></ul><ul><li>Integer i3 = Integer.valueOf(“012”,8); </li></ul><ul><li>Integer i4 = Integer.valueOf(“102”); </li></ul>
  7. 7. Class Hierarchy java.lang.Object java.lang.Number java.lang.Byte java.lang.Long java.lang.Float java.lang.Integer java.lang.Short java.lang.Boolean java.lang.Double java.lang.Character
  8. 8. java.lang.Number <ul><li>public java.lang.Number(); </li></ul><ul><li>public abstract int intValue(); </li></ul><ul><li>public abstract long longValue(); </li></ul><ul><li>public abstract float floatValue(); </li></ul><ul><li>public abstract double doubleValue(); </li></ul><ul><li>public byte byteValue(); </li></ul><ul><li>public short shortValue(); </li></ul>java.lang.Object java.lang.Number
  9. 9. Wrapper Conversion Utilities using methods from Number class
  10. 10. Wrapper Conversion Utilities contd. <ul><li>Integer i = new Integer(200); </li></ul><ul><li>byte b = i.byteValue(); </li></ul><ul><li>short s = i.shortValue(); </li></ul><ul><li>long l = i.longValue(); </li></ul><ul><li>float fl = i.floatValue(); </li></ul><ul><li>Float f = new Float(5.6f); </li></ul><ul><li>byte b2 = f.byteValue(); </li></ul><ul><li>short s2 = f.shortValue(); </li></ul><ul><li>int i2 = f.intValue(); </li></ul>Stack Heap 5.6 200 i f i2 b2 s2 fl l s b
  11. 11. Wrapper Conversion Utilities contd.
  12. 12. Boxing & Unboxing <ul><li>Integer ref = new Integer(15); </li></ul><ul><ul><li>Encapsulating the primitive value in the respective Wrapper object is called boxing or Wrapping. </li></ul></ul><ul><li>int a = ref.intValue() + 5; </li></ul><ul><ul><li>Getting the encapsulated value out of the object is called unboxing or unwrapping . </li></ul></ul>
  13. 13. AutoBoxing & AutoUnboxing <ul><li>Supported by jdk1.5+ </li></ul><ul><li>Integer i = 67; </li></ul><ul><li>automatically translated into following code by the compiler. </li></ul><ul><ul><ul><li>Integer i = new Integer(67); // so called AutoBoxing </li></ul></ul></ul><ul><li>int a = i + 10; </li></ul><ul><li>automatically translated into following code by the compiler. </li></ul><ul><ul><ul><li>int a = i.intValue() + 10; // so called AutoUnboxing </li></ul></ul></ul>