Wrapper Class in Java
INTRODUCTION
Wrapper classes for the primitive data types were introduced in order to make Java a pure object oriented language. Before the introduction of wrapper class, Java was not a fully object oriented language because of the primary data types, e.g., int x.
Here x is not an object. In Java, an individual class is provided for every primitive data type. However, primitive data type could be converted into object type by the use of wrapper classes present in the java. lang package. Features like autoboxing and unboxing were also available to make Java a purely object oriented language.
GETTING INTO THE CONCEPT
Each Java primitive data type has a corresponding wrapper class. When an object of the wrapper class is created, it contains a field where primitive data types are stored.
Significance Of Wrapper Classes
The wrapper classes are required because of the following reasons:
• Vector, ArrayList, Linkedlist, classes present in java .util package cannot handle primitive data types like int, char, float, etc. Hence primitive data types may be converted into object types by using wrapper classes present in java. lang package.
Wrapper classes convert primitive data types into objects.
Table :- Primitive data types and corresponding wrapper classes,
Primitive Data Types | Wrapper Class |
boolean | Boolean |
byte | Byte |
short | Short |
char | Character |
int | Interger |
float | Float |
long | Long |
double | Double |
REMEMBER
- Wrapper class is a wrapper around a primitive data type, which represents primitive data types in their corresponding class instances. For example, a Boolean data type can be represented as a Boolean class instance.
- All of the primitive wrapper classes in ava are immutable, i.e., once a value is assigned to a wrapper class instance, it cannot be changed further.
- Wrapper classes are broadly used with collection classes in the java . ut i l package and with the classes in the java . lang .reflect reflection package.
- The primitive types and the corresponding wrapper classes are as follows:
Wrapper Class in Java
Primitive | Wrapper |
boolean | java. lang. Boolean |
byte | java. lang. Byte |
char | java. lang. Character |
double | java. lang. Double |
float | java. lang. Float |
int | java. lang. Integer |
long | java. lang. Long |
short | java. lang. Short |
void | java. lang. Void |
Java 5.0 version introduced additional wrapper classes in the java . util . concurrent . atomic package. These classes are mutable and cannot be used as a replacement for the regular wrapper classes. Instead, they provide atomic operations for assignment, addition and increment. These classes act like variables and cannot be used as a substitute for the regular wrapper classes. Wrapper Class in Java
Primitive | Wrapper |
boolean | Atomic Boolean |
int | Atomic Integer, |
long | Atomic Long |
V | Atomic Reference <V> |
- Though not a wrapper class , the Void class in similar in that it provides an objects representation of the void return.
- The void class is an uninstantiable placeholder class used by the java . lang reflect API to hold a reference to the class object representing the java keyword void,
- All the methods of the wrapper classes are static,
- The wrapper class does not contain constructors.
- Once a value is assigned to a wrapper class instance , it cannot be changed,
Character Class
Character class object is a wrap around a char.
The constructor is :
Character ( Char ch )
Methods
The various methods of the character class are :
public static character valueof((char c)
This method converts a single character into a character class object.
public char charValue ()
This method is useful to convert a Character class obiect into a primitive char value.
public int hashCode ()
Returns the hash value of a Character class object.
public static String toString (char c)
This method converts char data types into String.
Also Read
Also Read :- Networking
Wrapper Class in Java