Wrapper Class
Wrapper classes provide a way to use primitive data types (int , boolean , etc.) as objects.
The table shows the primitive types and the equivalent wrapper classes.
Sometimes you must use wrapper classes, for example, when working with collection objects, such as ArrayList , where primitive types cannot be used (the list can only store objects):
|
|
|
// Invalid: no primitive type to be used for ArrayList
ArrayList<int> myNumbers = new ArrayList<int>( );
// Valid: Interger, a wrapper class of int
ArrayList<Integer> myNumbers = new ArrayList<Integer>( );
ArrayList
is a part of collection framework and is present in java.util
package.
It provides us dynamic arrays in Java.
It may be slower than standard arrays, but can be helpful in programs where intensive manipulation of the arrays is needed.
You can’t judge a book by its cover.
|