Wrapper Classes In JAVA
A Java wrapper class is explained in detail on this page with the goal of fulfilling educational requirements.
Java Wrapper Classes
Using Java wrapper classes, it becomes possible to use primitive data types as objects, such as ints, booleans and others.
As you can see in the table below, each primitive type is represented by the equivalent wrapper class in the Java language:
Primitive Data Type | Wrapper Class |
---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
While writing Java code, you sometimes have to use wrapper classes to work with Collection objects, such as ArrayLists, because primitive types are not allowed to be used in them (the list can only store objects); for example:
Consider the Following LinkedList.
We cannot use primitive data types directly as shown below:
import java.util.LinkedList;public class Main {
public static void main(String[] args) {LinkedList length_of_woods =new LinkedList(); // directly using primitive data type ( float )
}
}
import java.util.LinkedList;public class Main {
public static void main(String[] args) {LinkedList length_of_woods =new LinkedList(); // Here we use wrapper class Float instead of float
}
}
Similarly, While implementing HashMap we can not use:
import java.util.HashMap;public class Main {
public static void main(String[] args) {HashMap<int,double> my_HashMap= new HashMap<int,double>();}
}
Instead, we should utilize the wrapper class for int and double (primitive data types):
import java.util.HashMap;public class Main {
public static void main(String[] args) {HashMap<Integer,Double> my_HashMap= new HashMap<Integer,Double>();}
}
Create Wrapper Objects
Instead of using the primitive type to create a wrapper object, you can use the wrapper class to do so. If you print the object, you’ll get the value:
public class Main {
public static void main(String[] args) {
Integer first_odd_Num = 1;
Float individual_Height = 5.11f;
Character first_Alpha = 'A';
System.out.println("First odd number : "+first_odd_Num);
System.out.println("My Height: "+individual_Height);
System.out.println("First English Alphabet: "+first_Alpha);
}
}
public class Main {
public static void main(String[] args) {Double double_value=13.5124123213214d;
Boolean isJavaEasy=true;System.out.println("The value of double variable is: "+double_value);
System.out.println("isJavaEasy: "+isJavaEasy);}
}
Now that you’re dealing with objects, there are several methods you can use to find information regarding them.
As examples methods such as intValue(), byteValue(), shortValue(), longValue(), floatValue(), doubleValue(), charValue(), booleanValue() and so on can be used to obtain the value associated with the wrapper object.
The following example will produce the same result as the previous one:
public class Main {
public static void main(String[] args) {
Integer first_odd_Num = 1;
Float individual_Height = 5.11f;
Character first_Alpha = 'A';
System.out.println("First odd number : "+first_odd_Num.intValue());
System.out.println("My Height: "+individual_Height.floatValue());
System.out.println("First English Alphabet: "+first_Alpha.charValue());
}
}
public class Main {
public static void main(String[] args) {Double double_value=13.5124123213214d;
Boolean isJavaEasy=true;System.out.println("The value of double variable is: "+double_value.doubleValue());
System.out.println("isJavaEasy: "+isJavaEasy.booleanValue());}
}
In addition, you can transform wrapper objects into strings using the toString() method.
This example converts an integer into a String and outputs the length of the string using the length() method of the String class:
public class Main {
public static void main(String[] args) {
Float float_num =23.523f;
String my_To_String_value = float_num.toString();
System.out.println(my_To_String_value.length());
}
// Output : 6
}
public class Main {
public static void main(String[] args) {
Double double_num =78.24321412d;
String my_To_String_value = double_num.toString();
System.out.println("Length of toString: "+my_To_String_value.length());
}
// Length of toString: 11
}