Wrapper Classes
Wrapper Classes
Wrapper classes convert primitive data types into their respective objects
In Java, there are 8 primitive data types : byte, short, int, long, float, double, char, boolean
These primitive data types cannot be treated as objects until you convert them into objects by
using their respective wrapper class.
Each java primitive data type has a corresponding wrapper class in the java.lang
package
Wrapper classes serve the following 2 purposes:
1. Primitive data types, after they have been converted into objects can be used in Collection
2. Wrapper classes provide a variety of utility methods to convert any object into primitive
types (such as byte, int, float, booloean, and char) or vice versa.
The java.lang package is automatically imported to every source file.
This package contains the Object class, which is the parent of all classes.
Boolean, Character, and Number are child classes of the Object class which are also
wrapper classes
Number class is the parent of wrapper classes Byte, Short, Integer, Long, Float, and
Double that handles primitive values as objects
Object
Boolean boolean
Byte byte
Character char
Short short
Integer int
Long long
Float float
Double double
Can construct a wrapper class object by passing the value to be wrapped in to
the appropriate constructur.
Eg :
int p=42;
Integer i=new Integer(p); // BOXING
int p2=i.intValue(); //UNBOXING
In J2SE 5.0 the Autoboxing feature enables to assign and retrieve primitive types without
the need of the wrapper classes.
Eg :
int p;
integer i=p; // AUTOBOXING
int p2=i; // AUTOUNBOXING
The compiler will create the wrapper object automatically when assigning a primitive
to a variable of the wrapper class type.
The compiler will automatically extract the primitive value when assigning from a
wrapper object to a primitive variable.
1. The IntegerWrapper Class converts the value of the int primitive data type into an object of the
class.
2. The DoubleWrapper class converts the value of the double primitive type
into an object of this class
public class DoubleWrapper
{
public static void main(String args[])
{
double d=2.14;
Double val = new Double(d);
System.out.println (" Value of the Double object is " + val );
}
}
Program:
/* This program sums a list of numbers entered by the user. It converts the string
representation of each number into an int using parseInt(). */
import java.io.*;
class ParseDemo
{
public static void main(String args[]) throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
int i;
int sum=0;
System.out.println("Enter numbers, 0 to quit.");
do
{
str = br.readLine();
try
{
i = Integer.parseInt(str);
}
catch(NumberFormatException e)
{
System.out.println("Invalid format");
i = 0;
}
sum += i;
System.out.println("Current sum is: " + sum);
} while(i != 0);
}
}
Program:
/* Convert an integer into binary, hexadecimal, and octal.*/
class StringConversions
{
public static void main(String args[])
{
int num = 19648;
System.out.println(num + " in binary: " +
Integer.toBinaryString(num));
System.out.println(num + " in octal: " +
Integer.toOctalString(num));
System.out.println(num + " in hexadecimal: " +
Integer.toHexString(num));
}
}
Program:
class IsDemo
{
public static void main(String args[])
{
char a[] = {'a', 'b', '5', '?', 'A', ' '};
for(int i=0; i<a.length; i++)
{
if(Character.isDigit(a[i]))
System.out.println(a[i] + " is a digit.");
if(Character.isLetter(a[i]))
System.out.println(a[i] + " is a letter.");
if(Character.isWhitespace(a[i]))
System.out.println(a[i] + " is whitespace.");
if(Character.isUpperCase(a[i]))
System.out.println(a[i] + " is uppercase.");
if(Character.isLowerCase(a[i]))
System.out.println(a[i] + " is lowercase.");
}
}
}