What is a Wrapper class?
- Wrapper classes wrap primitive values in a class and offers utility to access them through objects.
- Some of the primitive wrapper data types are :
Byte, short, int, long, float, double, char, Boolean.
-
Example : Create a class name VectorAdd to populate it with integer values using the add(int, object) method.
public class VectorAdd
{
public static void main(String argv[])
{
Vector v = new Vector();
v.add(0,new Integer(10));
v.add(1,new Integer(20));
v.add(2,new Integer(30));
for(int i=0; i < v.size();i ++)
{
Integer iw =(Integer) v.get(i);
System.out.println(iw);
}
}
}
What is a Wrapper class?
Wrapper classes are classes that allow primitive types to be accessed as objects.