A wrapper class wraps a primitive value in an object.
Primitive Wrapper
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
A constructor of a wrapper class can take a primitive value as an argument.
A wrapper class, except the Character class, can take a String value as an argument.
<Creating objects>
e.g.
・ Double d = new Double(7.0);
・ Double d = Double.valueOf(7.0);
・ Double d = Double.valueOf("7.0");
・ Double d = Double.valueOf("7.0f");
・ Short s = d.shortValue(d);
<Getting a primitive value from a wrapper object>
e.g.
・ double value = Double.parseDouble("2.0");
・ double value = Double.parseDouble("2.0d");
<Comparison of objects>
Use the == operator or an equals method to compare objects.
The equals method defined in the Object class is overridden in the wrapper classes and the String class.
However, Object’s equals method and others overridden in the String class and the wrapper class are different in behavior.
The equals method in the Object class returns true if two objects are “equal”.
An equals method in the String class or wrapper classes returns true if two objects are “semantically equal”.
<Sample>
Object object1 = new Object();
Object object2 = new Object();
System.out.println("object1 == object2 : " + (object1 == object2));
System.out.println("object1.equals(object2) : " + object1.equals(object2));
Integer int1 = new Integer(5);
Integer int2 = new Integer(5);
System.out.println("int1 == int2 : " + (int1 == int2));
System.out.println("int1.equals(int2) : " + int1.equals(int2));
<Result>
object1 == object2 : false
object1.equals(object2) : false
int1 == int2 : false
int1.equals(int2) : true