首页 > 其他 > 详细

Chapter 8 Primitives as Types

时间:2014-03-01 07:16:07      阅读:545      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣

 

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:

bubuko.com,布布扣

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

Chapter 8 Primitives as Types

原文:http://javabeta.blog.51cto.com/4039428/1364945

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!