Java Wrapper Classes
Main Objective of wrapper classes in Java
To wrap primitives into object form so that we can handle primitives also just like objects.
To define several utility functions that are required for the primitives.
In java.lang package we have a separate class for each of the primitive data types namely Byte, Character, Double, Integer, Float, Long, Short.
At the time of instantiation, these classes accept a primitive datatype directly, or in the form of String.
You can add primitive datatypes to various Collection objects such as ArrayList, HashMap, etc Using wrapper classes.
You can pass primitive values over a network using wrapper classes.
Need Wrapper Class
- To convert primitive data types into objects. The objects are necessary if we wish to modify the arguments passed into the method (because primitive types are passed by value).
- Java.util package handles only objects and hence wrapper classes help to wrap primitive to object and vice-versa.
- Collection framework Classes such as ArrayList and Vector store only the objects and not the primitive types.
- The object is needed to support synchronization.
Constructors in Wrapper Classes
All most all wrapper classes define the following 2 constructors one can take corresponding primitive as an argument and the other can take String as an argument.
Integer i = new Integer(11);
Integer i = new Integer("11");
Wrapper class Constructor summary
Wrapper class | Constructor |
Byte | byte, String |
Short | short, String |
Integer | Int, String |
Long | long, String |
Float | float, String, double |
Double | double, String |
Character | char |
Boolean | boolean, String |
If the String is not properly formatted i.e., if it is not representing number then we will get runtime exception saying "NumberFormatException".
Example:package com.javacodestuffs.core.wrapper.classes; public class WrapperClas1 { public static void main(String[] args) throws Exception { Integer i = new Integer("ten"); System.out.println(i); //will get NumberFormatException } } output: Exception in thread "main" java.lang.NumberFormatException: For input string: "ten" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.
(Integer.java:867) at com.javacodestuffs.core.wrapper.classes.WrapperClass1.main(WrapperClass1.java:5)
Float class defines 3 constructors with float, String, and double arguments.
1) Float f = new Float(10.5f);
2) Float f = new Float("10.5f");
3) Float f = new Float(10.5);
4) Float f = new Float("10.5");
Character class defines only one constructor which can take char primitive as an argument there is no String argument constructor.
Character ch = new Character('a'); //valid
Character ch = new Character("a"); //invalid
The Boolean class defines 2 constructors with boolean primitive and String arguments.
If we want to pass boolean primitive the only allowed values are true, false where the case should be lower case.
Example:
Boolean b = new Boolean(true);
Boolean b = new Boolean(false);
//Boolean b1=new Boolean(True);//C.E
//Boolean b=new Boolean(False);//C.E
//Boolean b=new Boolean(TRUE);//C.E
If we are passing String argument then the case is not important and content is not important.
If the content is case insensitive String of true then it is treated as true in all other cases it is treated as false.
Example 1
package com.javacodestuffs.core.wrapper.classes;
public class WrapperClass2 {
public static void main(String[] args)throws Exception {
Boolean b1=new Boolean("true");
Boolean b2=new Boolean("True");
Boolean b3=new Boolean("false");
Boolean b4=new Boolean("False");
Boolean b5=new Boolean("ashok");
Boolean b6=new Boolean("TRUE");
System.out.println(b1);//true
System.out.println(b2);//true
System.out.println(b3);//false
System.out.println(b4);//false
System.out.println(b5);//false
System.out.println(b6);//true
}
}
output:
true
true
false
false
false
true
Example 2
package com.javacodestuffs.core.wrapper.classes;
public class WrapperClass3 {
public static void main(String[] args) throws Exception {
Boolean b1 = new Boolean("yes");
Boolean b2 = new Boolean("no");
System.out.println(b1); //false
System.out.println(b2); //false
System.out.println(b1.equals(b2)); //true
System.out.println(b1 == b2); //false
}
}
output:
false
false
true
false
Implementation of the wrapper class in Java
Autoboxing
package com.javacodestuffs.core.wrapper.classes;
import java.util.ArrayList;
import java.util.List;
public class AutoBoxingTest {
public static void main(String args[]) {
int num = 10; // int primitive
Integer obj = Integer.valueOf(num); // create a wrapper class object
List < Integer > list = new ArrayList < Integer > ();
list.add(11); // autoboxing
Integer val = 2; // autoboxing
System.out.println("num is : " + num);
System.out.println("obj is : " + obj);
System.out.println("num inside arrayList autoboxing " + list.get(0));
}
}
output:
num is : 10
obj is : 10
num inside arrayList autoboxing 11
Unboxing
Unboxing is used to convert the Wrapper class object into corresponding primitive data types. Conversion of Integer to int, Long to long, Double to double, etc.
package com.javacodestuffs.core.wrapper.classes;
public class Unboxing {
public static void main(String args[]) {
Integer obj = new Integer(10); // Creating Wrapper class object
int num = obj.intValue(); // wrapper object to primitive datatype
System.out.println("obj is: " + obj);
System.out.println("num is: " + num);
}
}
output:
obj is: 10
num is: 10
If we write a method that accepts a primitive value or wrapper object, we can still pass both values to them.
package com.javacodestuffs.core.wrapper.classes;
public class UnboxingTest {
public static void main(String args[]) {
Integer object = new Integer(1);
int val1 = getSquareValue(object); //unboxing
int val2 = object; //unboxing
System.out.println("val1 is: " + val1);
System.out.println("val2 is : " + val2);
}
public static int getSquareValue(int i) {
return i * i;
}
}
output:
val1 is: 1
val2 is : 1
Note :
1). In all wrapper classes toString() method is overridden to return its content.
2). In all wrapper classes .equals() method is overridden for content compression.
Integer i1 = new Integer(23);
Integer i2 = new Integer(23);
System.out.println(i1); //23
System.out.println(i1.equals(i2)); //true
Question/Articles related to Java Wrapper Classes
Wrapper class methods in java
Custom wrapper class in java
Advantage of wrapper class in java
Autoboxing in java
Unboxing in java
List all wrapper classes in java
In this article, we have seen the Java Wrapper Classes with examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment