Wrapper Classes
Wrapper Classes
Wrapper Classes
By
Arvind Kumar
Asst. Professor, LPU
Introduction
• These are known as wrapper classes, because they "wrap" the primitive
data type into an object of that class.
• They are one per primitive type: Boolean, Byte, Character, Double,
Float, Integer, Long and Short.
• 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.
What is the need of Wrapper Classes?
• To provide a mechanism to “wrap” primitive values in an
object so that the primitives can be included in activities
reserved for objects, like as being added to Collections, or
returned from a method with an object return value.
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean
Creating Wrapper Objects with the new
Operator
• Primitive datatype-->Wrapper Class-->Constructor arguments
• boolean--> Boolean--> boolean or String
• byte--> Byte--> byte or String
• char--> Character--> char
• short--> Short--> short or String
• int-->Integer--> int or String
• long--> Long--> long or String
• float-->Float--> float double or String
• double-->Double--> double or String
Difference b/w Primitive Data Type and
Object of a Wrapper Class
int x = 25;
int z = x + y; // wrong!
• The data field in an Integer object is only accessible using the
methods of the Integer class.
• Number declares methods that return the value of an object in each of the different
number formats. These methods are shown here:
byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )