Java Wrapper Classes Tutorial with Examples
This tutorial explains the concepts of Java wrapper classes with examples, shows how to convert from wrapper types to primitives and vice-a-versa, explains numeric wrapper classes and wraps up with character & boolean wrapper classes
Moreover, there are multiple data structures which do not allow primitives such as keys cannot be primitives in a HashMap, ArrayList elements cannot be primitives and so on. So, if we have primitive data values we can wrap them in an equivalent Wrapper type so that we can use them in all places where only Objects are allowed.
Primitive types and their corresponding Wrapper Types
Conversion of primitives to wrapper types via Wrapper Class Constructors (Common to all primitives)
Numeric Types
There are six numeric types -
This concludes the tutorial of wrapper classes where we covered why wrapper classes exist, what are the eight wrapper classes, common utility methods,features specific to numeric types and finally ending with details of Character & Boolean wrapper classes.
Character
.
What are wrapper classes
Java Wrapper Classes are used to hold primitive data type as Objects. In java primitive data types are not objects. Wrapper classes are wrappers which take a primitive data value and in a sense wrap it an Object. We can then pass these around wherever objects can be passed.Moreover, there are multiple data structures which do not allow primitives such as keys cannot be primitives in a HashMap, ArrayList elements cannot be primitives and so on. So, if we have primitive data values we can wrap them in an equivalent Wrapper type so that we can use them in all places where only Objects are allowed.
Primitive types and their corresponding Wrapper Types
Primitive | Wrapper Type |
---|---|
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
- Constructors with primitive as parameter: Parameterized constructors of wrapper classes which take corresponding primitive value as parameter can be used to create wrapper object instances.
For example:
Character characterObj=new Character('a');
and so on
Float floatObj=new Float(1.25);
Integer intObj=new Integer(100);
Boolean boolObj=new Boolean(true); - Constructors with String equivalent of primitive as parameter: Parameterized constructors which take String values to create their wrapper equivalents.
For example:
Integer intObj=new Integer("200");
and so on
Float floatObj=new Float("1.50");
Boolean boolObj=new Boolean("false");
- Converting Wrapper Objects to Strings: All wrapper classes override the
toString()
method to produce the equivalent String value.
For example:
String intStr=new Integer(100).toString();
→intStr
gets theString
value "100".
Note - The same methodtoString()
can be used to convert primitive values to String values by passing a primitive value to the method instead of a String value. - Converting Wrapper Objects to primitive values: Each wrapper type has a method type
Value()
. This method when applied to the Wrapper Type produces the equivalent primitive value.
For Example:
int intVal=new Integer(120).intValue();
→intVal
gets the primitiveint
value 120. - Converting Strings to Wrapper Objects:This is accomplished by using the
valueOf(String str)
method which is there in every wrapper class. Only character type does not have this method.
For Example:
Boolean booleanObj=String.valueOf("true");
and so on
Float floatObj=String.valueOf("0.25");
- Equals: All wrapper classes override the
equals()
method ofObject
class. Format of the equals method which is as defined in the Object class is -boolean equals(Object obj2)
- Hashcode:All wrapper classes override the
hashcode()
method ofObject
class. The hashcode value is calculated based on the primitive value in the wrapper class instance
Byte
, Short
, Int
, Long
, Float
and Double
out of the eight wrapper types. There are certain important features which are specific to, and found in, all numeric wrapper classes -
- Every numeric type has constants by the name
MIN_VALUE
&MAX_VALUE
which contain the minimum value and the maximum value possible for that wrapper type(which is same as the minimum & maximum values of corresponding primitive types). - All numeric wrapper classes have a static method
parseType(String s)
. This method takes a String value and returns a wrapper of the type Type passed to it.For example:int value=Integer.parseInt("100");//assigns a value 100 to int
Important Note - TheparseType(String s)
method throws aNumberFormatException
if the String passed to it cannot be parsed into an equivalent Type wrapper numeric value.
- Boolean wrapper class defines two constants
TRUE
&FALSE
which are equivalent to Boolean objects with true & false value respectively. - Character wrapper class has two constants
MIN_VALUE
&MAX_VALUE
which contain the minimum & maximum values possible for aCharacter
object. In addition, Character wrapper types also define some basic character utility methods liketoUpperCase()
&toLowerCase()
(self explanatory), and various methods to check whether the given character is uppercase/lowercase/titlecase/digit and to convert a given character to uppercase/lowercase/titlecase.
This concludes the tutorial of wrapper classes where we covered why wrapper classes exist, what are the eight wrapper classes, common utility methods,features specific to numeric types and finally ending with details of Character & Boolean wrapper classes.