Chapter 8 Primitives as Types

1) The primitive types and their corresponding Wrapper classes’ hierarchy:

QQ图片20140227181307

 

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:

QQ截图20140228103131

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

你可能感兴趣的:(style,classes,border,target,blank)