WrapperClasses
WrapperClasses
Wrapper classes
• primitive types, such as int or double hold the basic data types supported
by the language.
The type wrappers are Double, Float, Long, Integer, Short, Byte,
Character, and Boolean.
Offer many methods that allow us to fully integrate the primitive types into
Java’s object hierarchy.
Wrapper classes
Primitive datatypes can be converted into object types by using wrapper classes .
Character
Character is a wrapper around a char.
Character(char ch)
Here, ch specifies the character that will be wrapped by the Character object being created.
char charValue( )
Wrapper classes
Boolean
Boolean is a wrapper around boolean values.
Boolean(boolean boolValue)
Boolean(String boolString)
In the first version, boolValue must be either true or false.
In the second version, if boolString contains the string “true” (in uppercase or lowercase), then the new
Boolean object will be true. Otherwise, it will be false.
All of the numeric type wrappers inherit the abstract class Number.
Number declares methods that return the value of an object in each of the different number formats. These
methods are :
byte byteValue( )
short shortValue( )
int intValue( )
int longValue()
float floatValue()
double doubleValue( )
• All of the numeric type wrappers define constructors that allow an object to be
constructed from a given value, or a string representation of that value.
int i = iOb.intValue();
int i = iOb.intValue();
Wrapper classes
autoboxing and auto-unboxing
Beginning with JDK 5, Java added two important features: autoboxing and auto-
unboxing.
Below way to construct an Integer object that has the value 100:
Note : no object is explicitly created through the use of new. Java handles this automatically.
Wrapper classes
// Demonstrate autoboxing/unboxing.
class AutoBox
{
public static void main(String args[])
{
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
System.out.println(i + " " + iOb); // displays 100 100
}
}
Wrapper classes
• Inside main( ), m( ) is passed the value 100. Because m( ) is expecting an Integer, this
value is automatically boxed.
• Next, this int value is assigned to iOb in main( ), which causes the int return value to be
autoboxed.
Wrapper classes
++iOb;
Here iOb is unboxed, the value is incremented, and the result is reboxed.
Wrapper classes
Auto-unboxing allows to mix different types of numeric objects in an expression.
Once the values are unboxed, the standard type conversions are applied.
The result is reboxed and stored in dOb.
class AutoBox4
{ public static void main(String args[])
{ Integer iOb = 100;
Double dOb = 98.6;
dOb = dOb + iOb;
System.out.println("dOb after expression: " + dOb);
}
}
Wrapper classes
Because of auto-unboxing, we can use integer numeric objects to control a switch
statement.
For example, consider this fragment:
Integer iOb = 2;
switch(iOb)
{
case 1: System.out.println("one");
break;
case 2: System.out.println("two");
break;
default: System.out.println("error");
}
Wrapper classes
Autoboxing/Unboxing Boolean and Character Values
class AutoBox5
{ public static void main(String args[])
{
// Autobox/unbox a boolean.
Boolean b = true;
// b is auto-unboxed.
if(b) System.out.println("b is true");
// Autobox/unbox a char.
Character ch = 'x'; // box a char
char ch2 = ch; // unbox a char
System.out.println("ch2 is " + ch2);
}}
Wrapper classes
• Because of auto-unboxing, a Boolean object can now also be used to control loop
statements.
• When a Boolean is used as the conditional expression of a while, for, or
do/while, it is automatically unboxed into its boolean equivalent.
Boolean b;
// ...
while(b) { // ... }
Method calling Conversion action