Wrapper Class in Java

In Java, we have used primitive data types such as int, float, double, char etc. primitive data types are not objects and hence these data types can not be called by reference. If in some situation we need to use the primitive data types as object then what should we do.  Don’t worry! Java provide a wrapper class in order to use primitive data types as object, which encapsulates the primitive data type within an object.

Definition: Wrapper class which converts primitive data types into object types. 

The following table gives you overview of primitive data types and the corresponding wrapper class.



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



How to convert Strings to Numbers?

Ans: Use parse() method to convert Strings to Numbers.

Refer the following table.


Parse() Method
Meaning
1
int i = Integer.parseInt(String);
Converts String to integer
2
float f= Float.parseFloat(String);
Converts String to float
3
long l = Long.parseLong(String);
Converts String to long
4
double d = Double.parseDouble(String);
Converts String to double


How to convert Numbers to Strings?

Ans: Use toString() method to convert Numbers to Strings.

Refer the following table.



toString() Method
Meaning
1
String st = Integer.toString(i);
Converts integer to String
2
String st= Float.toString(f);
Converts  float to String
3
String st = Long.toString(l);
Converts long to String
4
String st = Double.toString(d);
Converts double to String