Which packages contain Wrapper class in Java?


Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wrap primitive datatypes within them. Following is the list of primitive data types and their respective classes −

Primitive datatypeWrapper class
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean

Package

Wrapper classes in Java belong to the java.lang package, Therefore there is no need to import any package explicitly while working with them.

Example

The following Java example accepts various primitive variables from the user and creates their respective wrapper classes.

 Live Demo

import java.util.Scanner;
public class WrapperClassesExample {
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter an integer value: ");
      int i = sc.nextInt();
      //Wrapper class of an integer
      Integer obj1 = new Integer(i);
      System.out.println("Enter a long value: ");
      long l = sc.nextLong();
      //Wrapper class of a long
      Long obj2 = new Long(l);
      System.out.println("Enter a float value: ");
      float f = sc.nextFloat();
      //Wrapper class of a float
      Float obj3 = new Float(f);
      System.out.println("Enter a double value: ");
      double d = sc.nextDouble();
      //Wrapper class of a double
      Double obj4 = new Double(d);
      System.out.println("Enter a boolean value: ");
      boolean bool = sc.nextBoolean();
      //Wrapper class of a boolean
      Boolean obj5 = new Boolean(bool);
      System.out.println("Enter a char value: ");
      char ch = sc.next().toCharArray()[0];
      //Wrapper class of a boolean
      Character obj6 = new Character(ch);
      System.out.println("Enter a byte value: ");
      byte by = sc.nextByte();
      //Wrapper class of a boolean
      Byte obj7 = new Byte(by);
      System.out.println("Enter a short value: ");
      short sh = sc.nextShort();
      //Wrapper class of a boolean
      Short obj8 = new Short(sh);
   }
}

Output

Enter an integer value:
254
Enter a long value:
4444445455454
Enter a float value:
12.324
Enter a double value:
1236.22325
Enter a boolean value:
true
Enter a char value:
c
Enter a byte value:
125
Enter a short value:
14233

Updated on: 10-Sep-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements