• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Primitive Wrappers
 

Primitive Wrappers

on

  • 821 views

 

Statistics

Views

Total Views
821
Views on SlideShare
821
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Primitive Wrappers Primitive Wrappers Presentation Transcript

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