Wrapper class in java
The wrapper class is used to store the data in the object. The primitive Datatype(int,float,...) data does not store in object. When you are want to store the data in object you can use Wrapper Class.
Collection Framework and Data Structure works only with objects .
Generics do not allow primitive Datatype. It allows only Wrapper Class. Example: ArrayList<Integer> num =new ArrayList<Integer>();
Primitive Data Type and Wrapper classes
Primitive Data Type | Wrapper Class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
Example-1
public class Main { public static void main(String[] args) { Integer num = new Integer(10); System.out.println(num); System.out.println(num.intValue()); } }
Output
10 10
Example-2
public class Main { public static void main(String[] args) { Double num = new Double(15); System.out.println(num); System.out.println(num.doubleValue()); } }
Output
15.0 15.0
Example-3
public class Main { public static void main(String[] args) { Character ch = new Character('H'); System.out.println(ch); System.out.println(ch.charValue()); } }
Output
H H