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
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
Conversion of primitives to wrapper types via Wrapper Class Constructors (Common to all primitives)
- 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.
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.
Integer intObj=new Integer("200");and so on
Float floatObj=new Float("1.50");
Boolean boolObj=new Boolean("false");
Utility Methods for conversion from wrapper type to primitives and vice-a-versa(Common to all primitives)
- Converting Wrapper Objects to Strings: All wrapper classes override the
toString()method to produce the equivalent String value.
String intStr=new Integer(100).toString();
Note – The same method
toString()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.
int intVal=new Integer(120).intValue();
intValgets the primitive
- 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.
Boolean booleanObj=String.valueOf("true");and so on
Equals & Hashcode in wrapper types(Common to all primitives)
- Equals: All wrapper classes override the
Objectclass. Format of the equals method which is as defined in the Object class is –
boolean equals(Object obj2)
- Hashcode:All wrapper classes override the
Objectclass. The hashcode value is calculated based on the primitive value in the wrapper class instance
There are six numeric types –
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
MAX_VALUEwhich 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 – The
parseType(String s)method throws a
NumberFormatExceptionif the String passed to it cannot be parsed into an equivalent Type wrapper numeric value.
Character & Boolean wrapper classes
Character and Boolean are some of the lesser used wrapper classes.
Boolean wrapper class defines two constants
FALSE which are equivalent to Boolean objects with true & false value respectively.
Character wrapper class has two constants
MAX_VALUEwhich contain the minimum & maximum values possible for a
Character object. In addition, Character wrapper types also define some basic character utility methods like
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.