In this section you will learn about Wrapper classes and all the methods that manipulate data and allows to operate a certain work.
Wrapper Classes
In this section you will learn about Wrapper
classes and all
the methods that manipulate data and allows to operate a certain
work.
Wrapper class is a wrapper around a primitive data type. It represents primitive data types in their
corresponding class instances e.g. a boolean data type can be represented as a
Boolean class instance. All of the primitive
wrapper classes in Java are immutable i.e. once assigned a value
to a wrapper class instance cannot be changed further.
Wrapper Classes are used broadly with Collection classes in the
java.util package and with the classes in the java.lang.reflect reflection
package.
Following table lists the primitive types
and the corresponding wrapper classes:
Primitive | Wrapper |
boolean | java.lang.Boolean |
byte | java.lang.Byte |
char | java.lang.Character |
double | java.lang.Double |
float | java.lang.Float |
int | java.lang.Integer |
long | java.lang.Long |
short | java.lang.Short |
void | java.lang.Void |
In Java 5.0 version, additional wrapper classes were introduced in the java.util.concurrent.atomic package. They provide atomic operations for assignment, addition and increment. These classes act like variables and cannot be used as a substitute for the regular wrapper classes. Few of these new wrapper classes like AtomicInteger and AtomicLong are the subclasses of the Number Classes.
Primitive | Wrapper |
boolean | AtomicBoolean |
int | AtomicInteger |
long | AtomicLong |
V | AtomicReference<V> |
Features Of the Wrapper Classes
Some of the sound features maintained by the Wrapper Classes are as under :
- All the methods of the wrapper classes are static.
- The Wrapper class does not contain constructors.
- Once a value is assigned to a wrapper class instance it
can not be changed, anymore.
Wrapper Classes : Methods with examples
There are some of the methods of the Wrapper class which are used to manipulate the data. Few of them are given below:
1. add(int, Object): Learn to insert an element at the specified position.
2. add(Object): Learn to insert an object at the end of a list.
3. addAll(ArrayList): Learn to insert an array list of objects to another list.
4. get(): Learn to retrieve the elements contained with in an ArrayList object.
5. Integer.toBinaryString(): Learn to convert the Integer type object to a String object.
6. size(): Learn to get the dynamic capacity of a list.
7. remove(): Learn to remove an element from a particular position specified by a index value.
8. set(int, Object): Learn to replace an element at the position specified by a index value.