As explained in the post primitive data types in Java there are eight primitive data types and most of the time you will use the primitive types in your code as it reduces the object creation overhead making it more efficient to use primitive types. But there are scenarios when you would want to use objects in place of primitives for that Java platform provides wrapper classes for each of the 8 primitive data types. These classes "wrap" the primitive in an object thus the name wrapper classes. Note that all the wrapper classes in Java are immutable.
Java Wrapper Classes
Eight wrapper classes used to wrap primitive data types are as given below-
Primitive Type | Type Wrapper class |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
Note that 6 of these are numeric and numeric wrapper classes are subclasses of the abstract class Number class in Java:
When do we need wrapper classes in Java
You need to use wrapper classes when you want an object holding primitive data, some of the scenarios where you will need wrapper classes are–
- You want to add primitive value in an Object[] array.
- You want to add primitive type to any collection like ArrayList, HashMap as you can add only objects to collection classes.
- You want to use any of the utility function provided by the wrapper classes for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).
Java Wrapper classes examples
- If you want to convert int to a float number.
In Integer class there is a floatValue() method that can be used for the purpose.
int num = 25; Integer i = new Integer(num); float fNum = i.floatValue(); System.out.println("float Value " + fNum);
- If you want to
convert double value to a string.
double d = 25.67; String str = Double.toString(d); System.out.println("string " + str);
- If you want to know the min and max range of any type, like for integer
System.out.println("Integer min value " + Integer.MIN_VALUE); System.out.println("Integer max value " + Integer.MAX_VALUE);
Output
Integer min value -2147483648 Integer max value 2147483647
For double
System.out.println("Double min value " + Double.MIN_VALUE); System.out.println("Double max value " + Double.MAX_VALUE);
Output
Double min value 4.9E-324 Double max value 1.7976931348623157E308
Autoboxing and unboxing
Here autoboxing and unboxing in Java should get an honorable mention; autoboxing and unboxing feature was added in Java 5 and it converts primitive into object and object into primitive automatically. In many cases now you don’t need to convert using utility methods as it will happen automatically.
As example you can directly assign int value to an Integer object–
Integer i = 25;
Now conversion and method call (valueOf()) in this case will be done by compiler.
Equivalent code if you were converting yourself–int num = 25; Integer i = Integer.valueOf(num);
That's all for this topic Type Wrapper Classes in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment