Wrapper class in java

Wrapper class

A wrapper class is a class whose object wraps or contains a primitive data type. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data type.

Table contains the list of wrapper classes that are defined in java.lang packages. They are useful to convert the primitive data types into object form.

Primitive Data Type Wrapper Class
int Integer
char Character
byte Byte
boolean Boolean
long Long
float Float
double Double

Number class

Number is an abstract class whose subclasses are Byte, Integer, Long, Float and double. So the methods of Number class are commonly available in all these subclasses.

Number class methods
byte byteValue(): This method converts the calling object into byte value.
short shortValue(): This method converts the calling object into byte value.
int intValue():This method converts the calling object into int value.
long longValue():This method converts the calling object into long value.
float floatValue():This method converts the calling object into float value.
double doubleValue():This method converts the calling object into double value.

Wrapper classes hierarchy

Untitled

Example
public class test
{
public static void main(String args[])
{
Character a=’a';
Character i=Character.valueOf((char) a);
Character j=a;
System.out.println(a+” “+i+” “+j);
}
}

Output:
a a a

Leave a Reply

Your email address will not be published. Required fields are marked *