Wrapper classes are used to wrap around primitive data types such as int, char, boolean, etc. and convert them into objects. This allows you to use primitive data types as objects, which is useful in certain situations, such as when working with collections that require objects instead of primitives.
Here are the wrapper classes for each primitive data type in Java:
Here's an example of how you might use a wrapper class:
// converting an int to an Integer object int num = 10; Integer numObj = new Integer(num); // converting an Integer object to an int int num2 = numObj.intValue();
You can also use autoboxing and unboxing to automatically convert between primitives and wrapper classes. Autoboxing is the automatic conversion of a primitive to its corresponding wrapper class, while unboxing is the automatic conversion of a wrapper class to its corresponding primitive.
Here's an example of using autoboxing:
// using autoboxing Integer numObj = 10; // autoboxing int num = numObj; // unboxing
Wrapper classes also provide several useful methods, such as parseXXX methods for converting strings to primitive values and toString methods for converting primitive values to strings.