What are wrapper classes in java? | Usage of wrapper classes
In java, every primitive data type has a corresponding wrapper class. These wrapper classes can be used to represent a primitive value as an object. Since primitive values in java are not objects, therefore in order to manipulate these values as objects, java.lang package provides a wrapper class for each of the primitive data types. Every wrapper class is final and objects of these wrapper classes are immutable i.e. their state cannot be changed. The below table represents primitive data types with their corresponding wrapper class:
Primitive data type | Wrapper class |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
These wrapper classes contain methods for constructing and manipulating objects of primitive values along with the constants, fields, and methods to convert primitive values to wrapper objects and vice versa.
How to convert primitive values to wrapper objects?
A wrapper class constructor taking a primitive value as an argument can be used to
create wrapper objects. For instance:
Character charobj= new Character('\n');
Boolean boolobj=new Boolean(false);
Integer intobj= new Integer(2021);
Double doubleobj = new Double(5.61);
Here we have passed, primitive values in respective wrapper class constructors.
And the wrapper class objects can be used to manipulate, primitive values as an object.
However, these wrapper constructors are now deprecated from java 9 onwards. Therefore if you are using
java 9 or above then
you can assign primitive value to the wrapper reference variable as shown below:
i.e.
charobj='a';
boolobj=false;
intobj=2021;
doubleobj=5.61;
and then apply varied methods of respective wrapper classes to perform operations on wrapper objects.
How to convert Strings to wrapper objects?
Except for the 'Character' class, every other wrapper class defines a static method valueOf(String s)
that returns the wrapper object corresponding to the primitive value represented by the
String object passed as argument. Character class also defines valueOf(char c) method but that takes
only character value as parameter.
Below examples shows different wrapper objects created by passing string arguments to valueOf(String s) method :
Integer intvar=Integer.valueOf("2009");
Boolean boolvar=Boolean.valueOf("flase");
Character charvar=Character.valueOf('f');
Double dvar=Double.valueOf("5.89");
Byte bvar=Byte.valueOf("101");
Short shrvar=Short.valueOf("013");
Long lngvar=Long.valueOf("90000");
Integer intvar=Integer.valueOf("jayesh");// invalid, This will cause java.lang.NumberFormatException
Boolean boolvar=Boolean.valueOf("jayesh");//false
How to convert wrapper objects to strings?
There is a method 'toString()' in 'Object' class. This method is overridden by every wrapper class.
This overriding method returns a string object containing the string representation of the primitive value in the wrapper object.
String boolstr=boolvar.toString();
String chrStr = charvar.toString();
String doubleStr = dvar.toString();
String bytestr=bvar.toString();
String shortStr=shrvar.toString();
String lngstr=lngvar.toString();
How to convert wrapper objects to primitive values?
Each wrapper class has 'typeValue()' method that returns the primitive value in the wrapper object.
Here the 'type' represents the type of primitive value. For e.g.
charValue(), booleanValue(), intValue(), doubleValue() and so on.
boolean b=boolvar.booleanValue();
char c=charvar.charValue();
double d=dvar.doubleValue();
int i=intvar.intValue();
How to convert Strings to numeric values?
All numeric wrapper classes like - Byte, Short, Integer, Long, Float, Double are all
sub-classes of abstract class 'Number'.
Each numeric wrapper class defines a static method called 'parseType(String s)' that returns
the primitive numeric value represented by the String object passed as an argument.
Here the 'Type' represents the name of a numeric wrapper class.
byte b= Byte.parseByte("12");
int i= Integer.parseInt("2030");
double d= Double.parseInt("4.22");