Wrapper Classes

In java, when we work with numbers we use primitive data types such as int, float, double etc. However there are situations when we need to process numbers as objects.

As java is object oriented programming language it provides a class for each primitive data type so that they can also be used as an object whenever required.

Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes, because they "wrap" the primitive data type into an object of that class.

So, there is an Integer class that holds an int variable, there is a Double class that holds a double variable, and so on. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.

The wrapping is done by the compiler. If we use a primitive where an object is required, java automatically wrap it as an object. This process is known as Boxing.

Similarly when we use as object where a primitive is required, java automatically unwrap the object into primitive. This process is known as Un-boxing.

Since, we do not need to do Boxing and Un-boxing explicitly this feature in java is known as Auto-Boxing.

The following two statements illustrate the difference between a primitive data type and an object of a wrapper class:

int x = 25; //primitive type variable 
Integer y = new Integer(33); //object type variable. 

Here is an example of Boxing and Un-boxing

public class Test {
 public static void main(String args[]) {
  Integer x = 5; // boxes int to an Integer object        
  x = x + 10; // un-boxes the Integer to a int        

Wrapper class Architecture

All of the numeric wrapper classes are subclasses of the abstract class Number: which provides various methods for comparison and conversions.

Using wrapper classes instead of primitive help us in a way that the methods can be used to manipulate the objects which is lacking in primitive types. Boolean and Character are subclass of Object class. wrapper-classes-java

Methods implemented by subclasses of Number class.

byte byteValue()

short shortValue()

int intValue()

long longValue()

float floatValue()

double doubleValue()

Converts the value of this Number object to the primitive data type returned.

int compareTo(Byte anotherByte)

int compareTo(Double anotherDouble)

int compareTo(Float anotherFloat)

int compareTo(Integer anotherInteger)

int compareTo(Long anotherLong)

int compareTo(Short anotherShort)

boolean equals(Object obj

Compares this Number object to the argument.

boolean equals(Object obj)

Determines whether this number object is equal to the argument.

The methods return true if the argument is not null and is an object of the same type and with the same numeric value.

Each Number class contains other methods that are useful for converting numbers to and from strings and for converting between number systems.

The following table lists these methods in the Integer class. Methods for the other Number subclasses are similar:



static Integer decode(String s)

Decodes a string into an integer. Can accept string representations of decimal, octal, or hexadecimal numbers as input.

static int parseInt(String s)

Returns an integer (decimal only).

static int parseInt(String s, int radix)

Returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.

String toString()

Returns a String object representing the value of this Integer.

static String toString(int i)

Returns a String object representing the specified integer.

static Integer valueOf(int i)

Returns an Integer object holding the value of the specified primitive.

static Integer valueOf(String s)

Returns an Integer object holding the value of the specified string representation.

static Integer valueOf(String s, int radix)

Returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix. For example, if s = "333" and radix = 8, the method returns the baseten integer equivalent of the octal number 333.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions