Wrapper classes were introduced with the discussion of the primitive data types. Primitive values in Java are not objects. In order to manipulate these values as objects, the java.lang package provides a wrapper class for each of the primitive data types. All wrapper classes are final. The objects of all wrapper classes that can be instantiated are immutable, that is, their state cannot be changed.
Although the Void class is considered a wrapper class, it does not wrap any primitive value and is not instantiable. It just denotes the Class object representing the keyword void.
In addition to the methods defined for constructing and manipulating objects of primitive values, the wrapper classes also define useful constants, fields, and conversion methods.
When working with numbers, most of the time you use the primitive types in your code. For example:
int i = 500; float gpa = 3.65f; byte mask = 0xff;
There are, however, reasons to use objects in place of primitives, and the Java platform provides wrapper classes for each of the primitive data types. These classes “wrap” the primitive in an object. Often, the wrapping is done by the compiler—if you use a primitive where an object is expected, the compiler boxes the primitive in its wrapper class for you. Similarly, if you use a number object when a primitive is expected, the compiler unboxes the object for you.
All of the numeric wrapper classes are subclasses of the abstract class
Wrapper class coreesonding to their respective primitives
System.out.println(a[i] + “is a digit “);
System.out.println(a[i] + “is a letter “);
Byte class methods:
byteValue() – returns the Byte value as byte value
Ex : byte bvalue = Byte.byteValue();
parseByte() – returns byte value from a byte string
Ex: byte bvaue = Byte.parseByte(“93”);
Integer class methods:
intValue() – returns the Integer value as int value
Ex: int bvalue = Integer.intValue();
parseInt() – returns int value from a int string
Ex: int bvaue = Integer.parseInt(“73”);
Usage of Wrapper Classes:
They should be used in situations when primitives cannot be used. The main situation is when you want to store primitives in one of the Java collections (which cannot handle primitives).
The use of the wrapper classes is greatly simplified by something called autoboxing. This means that Java automatically determines whether a primitive or a corresponding wrapper should be used. So it’s rare that you need to use the wrapper classes explicitly.
Integer is the wrapper for int. Thanks to autoboxing you can now store ints in the above Arraylist. Or rather Java will perform all necessary conversions back and forth between Integer and int.
Reason of using 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 resons 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 as a collection. Also we can pass them around as method parameters where a method expects an object.