Showing posts with label datatypes. Show all posts
Showing posts with label datatypes. Show all posts

Thursday 28 January 2016

Wrapper classes in java


In Java there are eight primitive datatypes and they are as below and they are called as Wrapper classes. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.

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

Importance of Wrapper classes
There are mainly two uses with wrapper classes.
  1. To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
  2. To convert strings into data types (known as parsing operations), here methods of type parsexxx() are used.
Following is the hierarchy of the above classes.



Difference between a primitive data type and an object of a wrapper class: 

int a = 10; // declares an int variable named a and initializes it with the value 10
Integer b= new Integer(20); // The object is initialized with the value 20 and a reference to the object is assigned to the object variable b.

In simple way this is wrapper class.