SlideShare a Scribd company logo
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

More Related Content

PPTX
What\'s New in C# 4.0
PPTX
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
PPT
Presentation
PPTX
constructor & destructor in cpp
PPTX
Objective C Primer (with ref to C#)
PPTX
Pointer and polymorphism
PPT
Lecture5
PPTX
Basic c++ programs
What\'s New in C# 4.0
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Presentation
constructor & destructor in cpp
Objective C Primer (with ref to C#)
Pointer and polymorphism
Lecture5
Basic c++ programs

What's hot (20)

PPTX
Functions in c++,
PPTX
Operator Overloading & Type Conversions
PDF
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
PDF
Virtual Functions
PPTX
Compile time polymorphism
PPTX
Operator Overloading
PPT
friends functionToshu
PPTX
Polymorphism in c++(ppt)
PPTX
operator overloading
PPT
Operator overloading
PPTX
C++ concept of Polymorphism
PDF
科特林λ學
PPT
Overloading
PDF
Deepak Soni,BCA 2nd Year
PPTX
C++ programming function
PPTX
Function overloading
PPT
C++ Function
PDF
Java Script Workshop
PPT
Polymorphism
PDF
GC in C++0x [eng]
Functions in c++,
Operator Overloading & Type Conversions
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
Virtual Functions
Compile time polymorphism
Operator Overloading
friends functionToshu
Polymorphism in c++(ppt)
operator overloading
Operator overloading
C++ concept of Polymorphism
科特林λ學
Overloading
Deepak Soni,BCA 2nd Year
C++ programming function
Function overloading
C++ Function
Java Script Workshop
Polymorphism
GC in C++0x [eng]
Ad

Similar to Primitive Wrappers (20)

PPT
Polymorphism in C++ for beginners reference
PPTX
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
PPT
OBJECTS IN Object Oriented Programming .ppt
PPT
pointers, virtual functions and polymorphisms in c++ || in cpp
PDF
Lecture20 vector
PDF
Threads and Callbacks for Embedded Python
DOCX
Java level 1 Quizzes
PPTX
RTTI and Namespaces.pptx ppt of c++ programming language
PPS
Wrapper class
PDF
麻省理工C++公开教学课程(二)
PPTX
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
PPT
Wrapper_Classes.ppt.....................
PDF
Kotlin for Android Developers - 3
PPTX
Wraper class slide in advance Java programming
PDF
Writing native bindings to node.js in C++
PPT
C Language fundamentals hhhhhhhhhhhh.ppt
DOCX
C questions
PPTX
Polymorphismupload
PPTX
Using Reflections and Automatic Code Generation
PPTX
04 Variables
Polymorphism in C++ for beginners reference
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
OBJECTS IN Object Oriented Programming .ppt
pointers, virtual functions and polymorphisms in c++ || in cpp
Lecture20 vector
Threads and Callbacks for Embedded Python
Java level 1 Quizzes
RTTI and Namespaces.pptx ppt of c++ programming language
Wrapper class
麻省理工C++公开教学课程(二)
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Wrapper_Classes.ppt.....................
Kotlin for Android Developers - 3
Wraper class slide in advance Java programming
Writing native bindings to node.js in C++
C Language fundamentals hhhhhhhhhhhh.ppt
C questions
Polymorphismupload
Using Reflections and Automatic Code Generation
04 Variables
Ad

More from Bharat17485 (12)

PPT
Channel Based Io
PPT
Core Java
PPT
Developing Multithreaded Applications
PPT
Interfaces & Abstract Classes
PPT
PPT
Exceptions & Its Handling
PPT
Jstl & El
PPT
Regular Expression
PPT
Stream Based Input Output
PPT
String Handling
PPT
PPT
Applying Generics
Channel Based Io
Core Java
Developing Multithreaded Applications
Interfaces & Abstract Classes
Exceptions & Its Handling
Jstl & El
Regular Expression
Stream Based Input Output
String Handling
Applying Generics

Primitive Wrappers

  • 2. 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.
  • 3. 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.
  • 5. 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”);
  • 6. 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”);
  • 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. 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
  • 9. Wrapper Conversion Utilities using methods from Number class
  • 10. 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
  • 12. 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 .
  • 13. 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