You are on page 1of 7

Wrapper Classes in Java

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 Character Number Void String StringBuffer StringBuilder

Byte Short Integer Float Long Short

Primitive Data Types and their Respective Wrapper Classes:

WRAPPER PRIMITIVE DATA


TYPE TYPE

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

BOXING: Change the primitive data types to their object equivalents

UNBOXING: Get the primitive type from the object reference

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.

public class IntegerWrapper


{
public static void main(String args[])
{
int n=25;
Integer val = new Integer(n);
System.out.println (" Value of the Integer object is " + val );
}
}

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 );
}
}

3. The CharacterWrapper class converts the value of the char primitive type
into an object of this class
public class CharacterWrapper
{
public static void main(String args[])
{
char c='D';
Character val = new Character(c);
System.out.println (" Value of the character object is " +
val );
}
}
4. The BooleanWrapper class converts the value of the boolean primitive
type into an object of this class
public class BooleanWrapper
{
public static void main(String args[])
{
boolean b=true;
Boolean val = new Boolean(b);
System.out.println (" Value of the Boolean object is " +
val);
/* Value stored in the boolean primitive type is case sensitive and must
not be stored within */
}
}

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.");
}
}
}
Number Methods
Following is the list of the instance methods that all the subclas ses of the
Number class implements

Sr.No. Method & Description

xxxValue()
1
Converts the value of this Number object to the xxx data type and returns it.

compareTo()
2 Compares this Number object to the argument.

equals()
3 Determines whether this number object is equal to the argument.

valueOf()
4 Returns an Integer object holding the value of the specified primitive.

toString()
5 Returns a String object representing the value of a specified int or Integer.

parseInt()
6 This method is used to get the primitive data type of a certain String.

abs()
7 Returns the absolute value of the argument.

ceil()
8 Returns the smallest integer that is greater than or equal to the argument. Returned as a double.

floor()
9 Returns the largest integer that is less than or equal to the argument. Returned as a double.

rint()
10 Returns the integer that is closest in value to the argument. Returned as a double.

round()
11 Returns the closest long or int, as indicated by the method's return type to the argument.

min()
12 Returns the smaller of the two arguments.
max()
13 Returns the larger of the two arguments.

exp()
14 Returns the base of the natural logarithms, e, to the power of the argument.

log()
15 Returns the natural logarithm of the argument.

pow()
16 Returns the value of the first argument raised to the power of the second argument.

sqrt()
17 Returns the square root of the argument.

sin()
18 Returns the sine of the specified double value.

cos()
19 Returns the cosine of the specified double value.

tan()
20 Returns the tangent of the specified double value.

asin()
21 Returns the arcsine of the specified double value.

acos()
22 Returns the arccosine of the specified double value.

atan()
23 Returns the arctangent of the specified double value.

atan2()
24 Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.

toDegrees()
25 Converts the argument to degrees.

toRadians()
26 Converts the argument to radians.

27 random()
Returns a random number.

Character Methods

Following is the list of the important instance methods that all the subclasses of the Character class
implement

Sr.No. Method & Description

isLetter()
1
Determines whether the specified char value is a letter.

isDigit()
2 Determines whether the specified char value is a digit.

isWhitespace()
3 Determines whether the specified char value is white space.

isUpperCase()
4 Determines whether the specified char value is uppercase.

isLowerCase()
5 Determines whether the specified char value is lowercase.

toUpperCase()
6 Returns the uppercase form of the specified char value.

toLowerCase()
7 Returns the lowercase form of the specified char value.

toString()
8 Returns a String object representing the specified character value that is, a one-character string.

You might also like