|
Answer» Wrapper classes are predefined classes in Java whose objects have primitive data types. They convert primitive data types into objects and vice versa. The Wrapper Classes provide a NEW angle to Java which helps it put a STRONG foot forward against its contemporaries. Data Structures in a collection framework can store only objects and not primitive data types. They provide SYNCHRONIZATION during multithreading. They are defined in the java.lang package. They are converted to primitive data types and vice versa by the process of boxing and unboxing. Typically, there are eight wrapper classes. They are linked to the primitive data types as follows: | Primitive Data types | Wrapper Classes |
|---|
| byte | Byte | | short | Short | | int | Integer | | long | Long | | char | Character | | bool | Boolean | | float | Float | | DOUBLE | Double |
Now let us discuss about the wrapper classes: - Byte: It wraps a primitive byte data type in an object. Its object contains a single field of type byte.
- Character: It wraps a char primitive data type in an object. An object of type Character contains a single field, whose type is char.
- Short: The short class wraps a primitive short type value in an object. Its object contains only a single field whose type is short.
- Integer: The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int.
- Long: The Long class generally wraps the primitive data type long into an object. An object of Long class contains a field with the type Long.
- Float: The Float class wraps a primitive float type value in an object. Its object contains only a single field whose type is float.
- Double: The Double class wraps a primitive double type value in an object. Its object contains only a single field whose type is double. This class is useful in providing various methods like a method which can be USED to convert a double to a String and vice versa.
- Boolean: It wraps a primitive boolean data type ‘bool’ in an object. Its object contains only a single parameter which is either true or false.
|