Please wait ......

Wrapper classes for primitive data type

Learn about wrapper classes for primitive data type


We learnt about the 8 primitive data types in Java namely byte, short, int, long, float, double, boolean and char.

Very often we need to convert String values to any of these data type. As an example in the below program, we need to convert the input which is in the String Array to int values. We use Integer class to do the conversion.

 public static void main(String[] args) {
        int numberOfStudents=Integer.parseInt(args[0]);
    }

 

Wrapper class:

Integer is a wrapper class for the primitive data type int.

Wrapper classes contains methods to have Object representation for primitive data types and thus provide related methods (useful for conversion and other utilities).

The wrapper classes are

  1. byte - Byte
  2. short - Short
  3. int - Integer
  4. long - Long
  5. float - Float
  6. double - Double
  7. char - Character
  8. boolean - Boolean

You can create an object of a wrapper class as below.

   public static void main(String[] args) {
        String value="544.5587";
        Double valueAsDouble=new Double(value);
    }