In Java, a wrapper class is a class that wraps (or encloses) a primitive data type and provides it with additional behavior and functionality. Wrapper classes are used to convert primitive data types into objects and vice versa.
There are eight wrapper classes in Java:
- Byte
- Short
- Integer
- Long
- Float
- Double
- Boolean
- Character
These wrapper classes provide several methods to perform various operations on the primitive data types they wrap. For example, you can use the parseInt()
method of the Integer
wrapper class to convert a string representation of an integer into an int
value. You can also use the toString()
method of any wrapper class to convert a primitive value into a string.
Wrapper classes are useful when you need to perform object-oriented operations on primitive data types, such as adding them to collections or passing them as method arguments. They also allow you to use primitive data types in contexts where only objects are allowed, such as when using the synchronized
keyword in a multi-threaded environment.