1) The primitive types and their corresponding Wrapper classes’ hierarchy:
2) Given that the wrapper classes are immutable, two objects with the same value can be used interchangeably, and there is no need to actually create two distinct objects. This fact is exploited by requiring that boxing conversions for certain value ranges of certain types always yield the same object. Those types and value ranges are:
Example:
public class WrapperTest {
static boolean test(Integer a, Integer b){ return a == b; }
public static void main(String[] args) {
System.out.println(test(3,3)); // in range [-128, 127], so identical object reference
System.out.println(test(3, new Integer(3))); // boxing coversion and creating new object reference
System.out.println(test(128, 128)); // out of range[-128, 127], so they are different object reference
}
}Output:
true
false
false
Chapter 8 Primitives as Types,布布扣,bubuko.com
原文:http://javabeta.blog.51cto.com/4039428/1364945