Why Wrapper classes ?
Collection is an object, it is able to manage a group of other objects.
Collection is an object, it is able to store other objects, that is the Collection object is able to have the reference values of the other objects.
In Java applications, Collection objects are able to store object reference values instead of the data.
If we want to store data inside the Collection object then we have to use the following steps.
- Declare the data in the form of the primitive data.
- Convert the data from Primitive Type to an Object form.
- Send the converted object to the Collection object.
If we want to read data from the Collection object we have to use the following steps.
- Get an object from the Collection.
- Convert the data from the Object form to the primitive type.
In the above two cases, to convert the data from primitive type to object type and from the object type to the primitive type we have to use a set of predefined classes called “Wrapper Classes”.
What Wrapper classes available ?
Java has provided all the wrapper classes in java.lang package with respect to the all primitive data types.
byte —------------> java.lang.Byte
short —-----------> java.lang.Short
int —-------------> java.lang.Integer
long —------------> java.lang.Long
float —-----------> java.lang.Float
double —----------> java.lang.Double
char —------------> java.lang.Character
boolean —---------> java.lang.Boolean
Java has provided all the wrapper classes as immutable classes, immutable classes are the java classes whose objects are not allowing modifications directly on their content, data is allowed for the modifications but the resultant modified data will not be stored in the original object, the resultant modified data will be stored by creating a new object of the same class.
Primitive types to Object Type
- By Using the parameterized constructors from all the wrapper classes:
public XXX(xxx value)
XXX —---> Wrapper class
xxx —---> Primitive Type
EX:
public class Main {
public static void main(String[] args){
int i = 10;
Integer integer = new Integer(i);
System.out.println(i +" "+integer);
}
}
- By using a static valueOf() method from the Wrapper classes.
public static XXX valueOf(xxx value)
XXX —----> Wrapper classes
xxx —----> Primitive Types
public class Main {
public static void main(String[] args){
int i = 10;
Integer integer = Integer.valueOf(i);
System.out.println(i +" "+integer);
}
}
- By using Auto-Boxing Mechanism
This mechanism was introduced in JAVA 1.5 version, in this mechanism we will not use any predefined constructor and any predefined method, here we will assign the Primitive data type variable to the wrapper class reference variable directly.
public class Main {
public static void main(String[] args){
int i = 10;
Integer integer = i; //autoboxing
System.out.println(i +" "+integer);
}
}
Object Type to Primitive types
- By Using xxxValue() method from all the wrapper classes:
public xxx xxxValue() → here, xxx : primitive Types
public class Main {
public static void main(String[] args){
Integer integer = new Integer(10);
int i = integer.intValue();
System.out.println(integer +" "+i);
}
}
- By Using Auto Unboxing
This mechanism was introduced in the JAVA 1.5 version.
In this mechanism , we will assign wrapper class reference variables to the respective primitive variables directly.
public class Main {
public static void main(String[] args){
Integer integer = new Integer(10);
int i = integer; // auto unboxing
System.out.println(integer +" "+i);
}
}
String type to the Object Type
- By Using String parameterized constructor from all the Wrapper classes:
public XXX(String value) → here, XXX : Wrapper classes.
public class Main {
public static void main(String[] args){
String str = "10";
Integer integer = new Integer(str);
System.out.println(str +" "+integer);
}
}
- By Using static valueOf() method:
public static XXX valueOf(String value) → here, XXX : Wrapper classes
public class Main {
public static void main(String[] args){
String str = "10";
Integer integer = Integer.valueOf(str);
System.out.println(str +" "+integer);
}
}
Object Type to String type
- By Using toString() method from all the wrapper classes:
public String toString()
public class Main {
public static void main(String[] args){
Integer integer = new Integer(10);
String str = integer.toString();
System.out.println(integer +" "+str);
}
}
- By Using ‘+’ concatenation operator
If we concatenate any reference variable with “” by using + then the JVM will execute the toString() method over the reference variable and concatenate the return value of the toString() method to the “”.
public class Main {
public static void main(String[] args){
Integer integer = new Integer(10);
String str = "" + integer;
System.out.println(integer +" "+str);
}
}
Primitive type to String Type
- By Using a static toString() method from all the wrapper classes:
public static String toString(xxx value) xxx : primitive data types
public class Main {
public static void main(String[] args){
int i = 10;
String str = Integer.toString(i);
System.out.println(i+" "+str);
}
}
- By Using ‘+’ concatenation operator
If we concatenate any primitive variable with “” by using ‘+’ operator then the resultant value will come under String data type only.
public class Main {
public static void main(String[] args){
int i = 10;
String str = "" + i;
System.out.println(i+" "+str);
}
}
String type to the Primitive type
- By Using static parseXxx() method from all the wrapper classes:
public static parseXxx(String value)
public class Main {
public static void main(String[] args){
String str = "10";
int i = Integer.parseInt(str);
System.out.println(str+" "+i);
}
}