Type Wrappers
There will be some situations where we have to store a data of primitive type in to a object like to pass or get the reference of object and as many of the data structures in java using objects and to store the result from that we have to use Objects which are called Type wrappers.
The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean.These classes offer a wide array of methods that allow you to fully integrate the primitive types into Java’s object hierarchy.
The Numeric Type Wrappers
- These are the wrappers which represent numeric values. These are Byte, Short, Integer,Long,Float, and Double. All these are inherit from the abstract class called Number which have the following methods to return the values of those corresponding objects:
byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )
- These will returns the value of an object in the corresponding type.
- All these numeric wrappers have constructors which allows an object to be constructed with a given value.
Integer(int num)
Integer(String str)
- If str does not contain a valid numeric value, then a NumberFormatException is thrown.
- There is no need to convert the Integer object to String as these methods will override the method toString() so we can use Integer object directly in println() statement as follows:
//example for numeric wrappers
public class NumericWrappersDemo
{
public static void main(String args[])
{
Integer iOb = new Integer(100); //using int type parameter constructor
Integer iOb1 = new Integer("300"); //using String type parameter constructor
int i = iOb.intValue(); //using wrapper method intValue() to convert to int value of the object.
System.out.println("using intValue() method::"+i );
System.out.println("using direct object::"+iOb); //in println() using object reference direclty
System.out.println("using constructor with String parameter::"+ iOb1);
}
}
Output:
using intValue() method::100
using direct object::100
using constructor with String parameter::300
Boolean
- Boolean is a wrapper around boolean values. It defines the following constructors:
Boolean(boolean boolValue)
Boolean(String boolString)
- To obtain a boolean value from a Boolean object,
boolean booleanValue( )
//example for using Boolean wrapper method and constructors
public class BooleanWrapperDemo
{
public static void main(String args[])
{
Boolean iOb = new Boolean(true);
Boolean iOb1 = new Boolean("false");
boolean i = iOb.booleanValue();
System.out.println("using booleanValue() method::"+i );
System.out.println("using direct object::"+iOb);
System.out.println("using constructor with String parameter::"+ iOb1);
}
}
Output:
using booleanValue() method::true
using direct object::true
using constructor with String parameter::false
Character
Character is a wrapper around a char. The constructor for Character is
Character(char ch)
To obtain the char value contained in a Character object,
char charValue( )
//example for using character wrapper method and constructor
public class CharacterWrapperDemo
{
public static void main(String args[])
{
Character iOb = new Character('T');
char i = iOb.charValue();
System.out.println("using charValue() method::"+i );
System.out.println("using direct object::"+iOb);
}
}
Output:
using charValue() method::T
using direct object::T
Autoboxing
In beginning of JDK5 java added two important
Autoboxing and Autounboxing.
- Autoboxing
It is the process of automatically creating a wrapper object corresponding to the primitive that being assigned to the object no need to explicitly construct an object.
Ex:Integer iOb = 100; //no need to write like Integer iOb= new Integer(100); or("100"); using constructors.
Autounboxing
It is the process where the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed.
Ex:int i= iOb; //no need to write like int i = iOb.intValue()
- Uses of Autoboxing and Autounboxing
It removes the tedium of manually boxing and unboxing values.
helps to prevent errors.
Used in generics where we have to refer directly to objects.
Mainly in collection Framework.
//example for autoboxing and autounboxing
public class AutoBoxingAndUnboxing
{
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
}
}
Output:
100 100
Autoboxing In Methods
We can do autoboxing in methods also where it will change the corresponding primitive value to an Object and object in to primitive type as follows
// Autoboxing/unboxing takes place with method parameters and return values.
public class Simple
{
// Take an Integer parameter and return an int value;
static int m(Integer v)
{
return v ; // auto-unbox to int
}
public static void main(String args[])
{
// Pass an int to m() and assign the return valueto an Integer. Here, the argument 100 is autoboxed into an Integer. The return value is also autoboxed into an Integer.
Integer iOb = m(100);
System.out.println(iOb);
}
}
Output:
100
Autoboxing/Unboxing in Expressions
Within an expression, a numeric object is automatically unboxed. The outcome of the expression is reboxed, if necessary.
//example for using autoboxing and autounboxing in expressions
// Autoboxing/unboxing occurs inside expressions.
public class InsideExpressions
{
public static void main(String args[])
{
Integer iOb1;
Double iOb2;
double i;
iOb1 = 100;
System.out.println("Original value of iOb1: " + iOb1);
// The following automatically unboxes iOb, performs the increment and then reboxes the result back into iOb.
iOb1--;
System.out.println("After ++iOb1: " + iOb1);
// Here, iOb1 is unboxed, the expression is evaluated, and the result is reboxed and assigned to iOb2 which is Double type.
iOb2 = iOb1 + (iOb1 / 3)+1.78;
System.out.println("iOb2 after expression: " + iOb2);
// The same expression is evaluated, but the result is not reboxed.
i = iOb1 + (iOb1 / 3)+1.78;
System.out.println("i after expression: " + i);
//using in switch statement
switch(iOb1)
{
case 98:System.out.println("value is 98");
break;
case 99: System.out.println("value is 99");
break;
case 100:System.out.println("value is 100");
break;
}
}
}
Output:
Original value of iOb1: 100
After ++iOb1: 99
iOb2 after expression: 133.78
i after expression: 133.78
value is 99
Autoboxing/Unboxing Boolean and Character Values
Like numeric types we have wrappers to Boolean and Character values also so we can perform autoboxing and autounboxing on boolean and character values.
// Autoboxing/unboxing a Boolean and Character.
class OnBooleanAndCharacterValues
{
public static void main(String args[])
{
// Autobox/unbox a boolean.
Boolean b = true;
// Below, b is auto-unboxed when used in a conditional expression, such as an if.
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);
}
}
Output:
b is true
ch2 is x
like in if statement we can use boolean object in all control statements like while, do-while etc.
Autoboxing/Unboxing in Preventing Errors
In some situations where there will be errors while performing autobfoxing and autounboxing manually using byteValue(), intValue() etc functions as follows
// An error produced by manual unboxing.
class PreventingErrors
{
public static void main(String args[])
{
Integer iOb = 5005; // autobox the value 5005
int i = iOb.byteValue(); // manually unbox as byte !!!
System.out.println(i); // does not display 5005 !
}
}
Output:
-115
***using these processes is less efficient when compared to using primitive data types where they have more efficient to perform some actions like calculating area of Rectangle
Integer length, breadth, area;
length =5;
breadth = 8;
area = length * breadth;