Wrapper Classes: Sensitivity: Internal & Restricted
Wrapper Classes: Sensitivity: Internal & Restricted
• Understand cloning
The object representation of integer ‘i’ holding the value 10 will be:
Integer iref = new Integer(i);
Here, class Integer is the wrapper class wrapping a primitive data type i
For all the primitive data types, there are corresponding wrapper classes. Storing primitive
types in the form of objects affects the performance in terms of memory and speed.
Representing an integer via a wrapper takes about 12-16 bytes, compared to 4 in an actual
integer. Also, retrieving the value of an integer uses the method Integer.intValue().
The wrapper classes are very useful as they enable you to manipulate primitive data types.
Object
Boolea
Number Character
n
Doubl
Byte Short Integer Long Float
e
long ln=999;
Long lng=new Long(ln);
Long ls=new Long("666");
System.out.println("long value="+lng.longValue());
System.out.println("long value from string
version="+ls.longValue());
Output:
long value=999
long value from string version=666
Output:
short value=999
short value from string version=666
Wrong!!! Wrong!!!
Right!!!
byte b = 12;
Integer I1=(int)b;
XYZ cloneTest() {
try {
return (XYZ) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("Cloning Not Allowed");
return this;
}
}
}
Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 30
Example on cloning (Contd.).
class CloneDemo1 {
public static void main(String args[]) {
XYZ x1 = new XYZ(); Output :
XYZ x2; x1 : 10 20.0
x1.a = 10; x2 : 10 20.0
x1.b = 20; x1 : 100 200.0
x2 = x1.cloneTest(); // cloning x1 x2 : 10 20.0
System.out.println("x1 : " + x1.a + " " + x1.b);
System.out.println("x2 : " + x2.a + " " + x2.b);
x1.a = 100;
x1.b = 200;
System.out.println("x1 : " + x1.a + " " + x1.b);
System.out.println("x2 : " + x2.a + " " + x2.b);
}
} © 2017 Wipro wipro.com confidential 31
Sensitivity: Internal & Restricted
Summary
In this module, you were able to: