Wrapper classes in “Java”

Wrapper class in Java provides the structure to convert basic into object and object into basic.

Since J2SE 5.0, autoboxing and unboxing factor converts basic into an object and object into basic automatically. The automatic alteration of basic into an object is known as autoboxing and vice-versa unboxing.

All the wrapper classes are final classes.

The 8 classes of a java.lang package is admitted as wrapper classes in java. The list of 8 wrapper classes are given below:

     Primitive Type Wrapper class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

Wrapper class Example: Primitive to Wrapper


Wrapper class Example: Wrapper to Primitive

Wrapper class Example: Wrapper to Primitive

Usage of Wrapper classes

1) It is sometimes easier to deal with primitives as objects.

2) Moreover, most of the collection classes store objects and not primitive data types.

3) The wrapper classes provide many utility methods also.

Because of these reasons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around like a collection. Also, we can pass them around as method parameters where a method expects an object.

Features of the Java Wrapper Classes

1. Wrapper classes convert numeric strings into numeric values.

2. The way to store primitive data in an object.

3. The value of() method is available in all wrapper classes except Character.

4. All wrapper classes have type Value() method. This method returns the value of the object as its primitive type.

Happy Learning !!

Leave a Reply

Your email address will not be published. Required fields are marked *