Wrapper Classes
Wrapper Classes in Java: An Introduction
The wrapper class in Java covers the primitive data type, enabling the latter to create an object. In simple terms, wrapper classes convert primitive data types into objects and objects into primitive data types. Otherwise, primitive data types are not capable of forming objects.
Primitive and Wrapper Classes in Java
In Java, only user-defined data types can form objects. Primitive data types do not include classes and cannot create objects. However, they can create objects with the help of wrapper classes.
What is the Use of Wrapper Class in Java?
Wrapper classes in Java wrap the primitive data types and allow serialization and deserialization.
A) Wraps the Primitive Data Types
With their versatility, wrapper classes serve as a bridge, facilitating communication between one data type and another in Java programming.
B) Enables Serialization and Deserialization in Java
The wrapper class in Java makes the process of serialization and deserialization possible.
List of Wrapper Classes
Primitive Data Types
Wrapper Classes
- boolean
- byte
- char
- short
- int
- long
- float
- double
- Boolean
- Byte
- Character
- Short
- Integer
- Long
- Float
- Double
While the primitive data types start with a small letter, the wrapper class version of the primitive data type is in capitals.
Boolean Wrapper Class
public final class Boolean
The value of the boolean primitive datatype is wrapped inside the object of the Boolean wrapper class. Only a single field of boolean type is present inside the object of the Boolean wrapper class.
Advantages of Boolean Wrapper Class
The Boolean wrapper class includes several methods for enabling the conversion of String to boolean and boolean to String. Also, this particular class contains various other methods and constants that are useful while working with the boolean.
Byte Wrapper Class
public final class Byte
The value of the byte primitive datatype is wrapped inside the object of the Byte wrapper class. Only a single field of byte type is present inside the object of the Byte wrapper class.
Advantages of Byte Wrapper Class
The Byte wrapper class is not just a theoretical concept; it’s a practical tool. It includes methods that allow the programmer to easily convert String to byte and vice versa, making the coding tasks more efficient. Also, it provides various other methods and constants that are invaluable when working with the byte.
Integer Wrapper Class
public final class Integer
The value of the int primitive datatype is wrapped inside the object of the Integer wrapper class. Only a single field of int type is present inside the object of the Integer wrapper class.
Advantages of Integer Wrapper Class
The Integer wrapper class includes methods that allow the programmer to easily convert String to int and int to String, making the coding tasks more efficient. Also, it provides various other methods and constants that are invaluable when working with an int.
Wrapper Classes Methods in Java
a) Some methods of the Boolean wrapper class include,
booleanValue()
This method returns the Boolean object’s value as a boolean primitive.
toString()
Returns String objects that represents the value of Boolean.
toString(boolean b)
Returns String object that represents the specified boolean.
b) Some methods of the Byte wrapper class include,
byteValue()
This method returns the Byte’s value as a byte.
toString()
Returns String objects that represents the value of Byte.
toString(byte b)
Returns String object that represents the specified byte. Here, the string object is a new one.
C) Some methods of the Integer wrapper class include,
intValue()
This method returns the Integer’s value as an int.
toString()
Returns String objects that represents the value of an Integer.
toString(int i)
Returns String object that represents the specified integer.