自动装箱拆箱发生在基本类型和其包装型互操作的时候。
以前一直不知的拆箱和装箱是valueOf和xxValue的别名。是语法糖的一种
public static void main(String[] args) {
Integer i = 10;
Integer j = 10;
System.out.println(j == i); //(1) true update1.1 如果i是反序列化回来的,那么不论值为多少,都不能==,因为反序列会生成新对象,不会调用valueOf了
i = 10000;
j = 10000;
System.out.println(j == i); //(2) false
i = new Integer(10000);
System.out.println(i == 10000); //(3) true
System.out.println(10000 == i); //(4) true
System.out.println(i == 10000L);//(5) true
}
(1)处的结果是 true。
(2)处的结果是false。
(3)处的结果是true,true,true
(1),(2)处的结果不同让人惊讶。i = 10;和i=10000;发生了自动装箱,编译器自动编译成 Integer.valueOf(10)和Integer.valueOf(10000)。看看这个方法的实现。
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
看到某些Integer对象是有缓存的,范围是-128,到high。
再看看IntegerCachestatic final int high;
static final Integer cache[];
static {
final int low = -128;
// high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
high默认是127,可以通过integerCacheHighPropValue
调整,看看这个属性的说明
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. During VM initialization the
* getAndRemoveCacheProperties method may be used to get and remove any system
* properites that configure the cache size. At this time, the size of the
* cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>.
*/
// value of java.lang.Integer.IntegerCache.high property (obtained during VM init)
private static String integerCacheHighPropValue;
可以通过该参数调整缓存的范围 -XX:AutoBoxCacheMax=<size>
3,4,5处发生拆箱,编译之后其实调用了intValue,或者longValue。获得的是基本类型。
总结: 两个包装类型的值比较,最好不要用 ==
分享到:
相关推荐
语法高亮插件vue-syntax-highlight,语法高亮插件vue-syntax-highlight,语法高亮插件vue-syntax-highlight
jedit中独立出来的语法高亮组件,可以支持很多种语言的语法,还可以自己扩展。
Unexpected end of file in conditional started on line xxx ----从xxx 开始的条件语句尚未结束文件不能结束 Unknown assemble instruction ----------------未知的汇编结构 houjiuming Unknown option ---------...
语法高亮插件 https://github.com/zsh-users/zsh-syntax-highlighting
liquid-syntax-mode, sublime text的流 语法模式 Siteleaf 流 支持 Sublime 文本这是shopify液体语法,它是基于 Django的Djaniero包。安装建议: 使用包控制插件安装。 搜索"。siteleaf 流 语法"。手动安装或者:克隆...
Atom-tree-sitter-syntax-visualizer.zip,Syntax tree visualizer for tree-sitter树人语法可视化工具,atom是一个用web技术构建的开源文本编辑器。
sublime 中安装这个插件 vue-syntax-highlight 但是还是没有vue高亮 请问什么原因??
npm install react-syntax-highlighter --save 为什么要这样一个? React还有其他语法高亮器,为什么要使用它呢? 最大的原因是,所有其他原因都依赖于在componentDidMount和componentDidUpdate触发调用以突出显示...
Atom-one-dark-lite-syntax.zip,Atom syntax theme. one-dark but without the feeling of drowning in red一个深色lite语法主题,atom是一个用web技术构建的开源文本编辑器。
前端开源库-grasp-syntax-javascript掌握语法javascript,掌握查询引擎常用部分
语法着色大家对它都不陌生吧,下面为大家介绍的是jquery-syntax动态语法着色的具体实现,需要的朋友可以参考下
Java Methods-Java Syntax and Style.ppt
Atom-chester-atom-syntax.zip,Fork of the atom syntax theme chester-atom-syntax切斯特,atom是一个用web技术构建的开源文本编辑器。
Atom-atom-panda-syntax.zip,atom的panda语法主题。一个超小的,黑暗的语法主题。这是panda语法主题的最新版本。这是一个特别为Atom设计的黑色语法主题,带有微妙的颜色,很容易让人看到。,atom是一个用web技术构建...
java-syntax.vim 这是一个Vim插件,提供Java语法高亮显示。 此语法高亮比默认语法更好。安装使用dein.vim call dein#add ( ' uiiaoo/java-syntax.vim ' ) 如果您使用其他插件管理器,请按照其方式进行。 注意:如果...
crayon-syntax-highlighter 是一个 WordPress 代码高亮插件,可用于 PHP 项目下。支持多语言,多主体,多字体的高亮插件。用 PHP 和 jQuery 写成。 标签:crayon
sublime text3 对 proto buffer的语法高亮 源文件https://packagecontrol.io/packages/Protobuf%20Syntax%20Hightlighting
主要介绍了深入理解Java基础之try-with-resource语法糖,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
vim-nerdtree语法高亮这为大多数常用文件扩展名上的nerdtree添加了语法。 无论您是想轻松地查看新项目中发生的事情,尝试学习具有不同文件夹结构的新框架,还是想使NERDTree看起来更好,此插件都可以为您提供帮助。 ...