Tag Archives: Number

Java : Wrapper Classes


Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.

Wrapper classes  implements Comparable Interface that’s help while sorting list of Objects on natural order.

See Also:

Advantages of Wrapper Class

Wrapper classes helps where we can’t use primitive type values only need objects. Compiler automatically converts primitive type to correspoing Wrapper classes as required.
Below are some most common advantages of wrappe classes:

  • Wrapper class required to convert the primitive data types in to objects. (Objects required when handling with collections)
  • In Generics programming, can be used with classes only not with primitive types.
  • Package java.util(Collections) contains only classes which only handles objects.
  • In multi threading, we need object to support synchronization.
  • These classes in java.lang package so no need to explicitly import it.

The eight classes of java.lang package are known as wrapper classes in java. The list of eight wrapper classes are given below:

Primitive Type Wrapper Description
boolean Boolean The Boolean class wraps a value of the primitive type boolean in an object.
char Character The Character class wraps a value of the primitive type char in an object.
byte Byte The Byte class wraps a value of primitive type byte in an object.
short Short The Short class wraps a value of primitive type short in an object.
int Integer The Integer class wraps a value of the primitive type int in an object.
long Long The Long class wraps a value of the primitive type long in an object.
float Float The Float class wraps a value of primitive type float in an object.
double Double The Double class wraps a value of the primitive type double in an object.

Wrapper Type Class Hierarchy

Java Wrapper Classes for primitive Type
Java Wrapper Classes for Primitive Type

Note : The abstract class Number is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.

Now you have learned about primitive type and corresponding wrapper Type and it’s class hierarchy. In further section you will lean about Autoboxing and Unboxing i.e conversion from primitive type to wrapper class or wrapper class to primitive type.

Autoboxing/Unboxing (Java 5+)

  • Autoboxing: Automatically converts primitive type into object/Wrapper Class.
    int a=30;
    //Manually covert int to Integer
    Integer k=Integer.valueOf(a);
    //Autoboxing, compiler will automatically convert to Integer and internally generate Integer.valueOf(a)
    Integer j=a;
    
  • Unboxing: Automatically converts object/Wrapper Class into primitive type.
    Integer a=new Integer(30);
    //Manually convert Integer to int
    int j=a.intValue();
    //Unboxing , compiler will automatically convert to int and internally generate a.intValue();
    int j=a;
    

From above example you can understand how internally conversion happen between primitive to wrapper class and wrapper class to primitive type.

Now here  corresponding to each wrapper class link you will get list of Constants, methods and exceptions.

References

https://docs.oracle.com/javase/8/docs/api/?java/lang/Integer.html
https://docs.oracle.com/javase/tutorial/java/data/numberclasses.htmlhttps://docs.oracle.com/javase/tutorial/java/data/numberclasses.html