Boxing and Unboxing

来源:互联网 发布:js时间戳转换日期函数 编辑:程序博客网 时间:2024/05/01 20:16

请预测下面程序运行的结果:

 

 

正确结果是:

True

False

 

你“目测”的结果对吗?

 

至于原因情况下面的这段E文解释:

A further subtlety is that boxed values may be cached. Caching is required when boxing an int or short value between -128 and 127, a char value between '/u0000' and '/u007f', a byte, or a boolean; and caching is permitted when boxing other values. This is because 6 is smaller than 128, so boxing the value 6 always returns exactly the same object.

 

Even for small values, for which == will compare values of type Integer correctly, we recommend against its use. It is clearer and cleaner to use equals rather than == to compare values of reference type, such as Integer or String.

 

原创粉丝点击