Wrapper Classes
Wrapper Classes
methods
By:
Srijana Basnet
Sonam Sherpa
Wrapper Class
A Wrapper Class is a class whose object wraps or contains primitive data types. Basically we wrap a primitive value into a wrapper
class object.
Need of Wrapper Classes:
1. They convert primitive data types into objects.
2. Wrapper classes in the java.util package handle only object, necessitating the use of wrapper classes.
3. Collection framework data structure like ArrayList and Vector can only store objects (reference types), not primitive types.
4. An object is necessary for synchronization in multithreading.
Primitive Data types and their corresponding Wrapper class
boolean Boolean char Character byte Byte short Short long Long float Float double Double int Integer
Autoboxing: It is the automatic transformation of primitive types into the object of the matching wrapper classes. Example: convert an int to an
Integer, short to Short etc.
Unboxing: It is a process of automatically converting a wrapper into its equivalent primitive type. Example: convert an Integer to an int, Byte to
byte etc.
Why to use a Number class object over Primitive data?
1. Compatibility with Collections: Many java collections(like ‘ArrayList’ or ‘HashMap’ can only store objects, not primitive types.
2. Flexibility with Null Values: Unlike primitive types, Number Objects can be ‘null’.
3. Convenience Methods: Number objects provide helpful method for conversion, comparison, and manipulation of numeric values which are
not directly available for primitive types.
4. Immutability and Identity: Number objects are immutable, meaning their values cannot be changed after creation which ensures data
integrity and simplifies debugging in complex applications.
Associate Methods
Some of the methods provided by wrapper classes in Java are:
1. xxxValue(): Here xxx represent primitive number data types and it 4. toString(): There are two variants of toString() method. They are
is used to convert the value of this Number Object to specified used to get String representation of a number. The other variants of
primitive data type. these methods are:
Example: - Integer.toBinaryString(int i)
Float num = Float.valueOf(10.45f); - Integer.toHexString(int i)
float res = num.floatValue(); // returns 10; - Integer.toOctalString(int i) which will return binary,
hexadecimal, octal representation of specified integer(i)
2. valueOf(): It allows us to create a wrapper object directly from respectively
primitive values. There are three variant of valueOf() method. Example:
- valueOf(xxx value) – xxx represents corresponding primitive type String s = Character.toString(‘A’); //returns A as String
- valueOf(String s) – only works for numeric primitive datatypes
- valueOf(String s, int radix) – radix returns decimal(10), octal(8) or 5. parsexxx(String s): xxx represents primitive datatype and it is
used to get primitive data type of a String.
hexadecimal(16) etc as output. //only works for int,short,byte,long
Example: Example:
Integer num = Integer.valueOf(10); //returns 10 as an object String str = “305”;
Integer num1 = Integer.valueOf(“A”,16); // returns 10 int num = Integer.parseInt(str);