Java基础教程之基本类型数据类型、包装类及自动拆装箱
前言
我们知道基本数据类型包括byte,short,int,long,float,double,char,boolean,对应的包装类分别是Byte,Short,Integer,Long,Float,Double,Character,Boolean。关于基本数据类型的介绍可参考Java基础(一)八大基本数据类型
那么为什么需要包装类?
JAVA是面向对象的语言,很多类和方法中的参数都需使用对象,但基本数据类型却不是面向对象的,这就造成了很多不便。
如:List
为了解决该问题,我们引入了包装类,顾名思义,就是将基本类型“包装起来“,使其具备对象的性质,包括可以添加属性和方法,位于java.lang包下。
拆箱与装箱
既然有了基本数据类型和包装类,就必然存在它们之间的转换,如:
publicstaticvoidmain(String[]args){
Integera=0;
for(inti=0;i<100;i++){
a+=i;
}
}
将基本数据类型转为包装类的过程叫“装箱”;
将包装类转为基本数据类型的过程叫“拆箱”;
自动拆箱与自动装箱
Java为了简便拆箱与装箱的操作,提供了自动拆装箱的功能,这极大地方便了程序员们。那么到底是如何实现的呢?
上面的例子就是一个自动拆箱与装箱的过程,通过反编译工具我们得到,
publicstaticvoidmain(String[]args){
Integera=Integer.valueOf(0);
for(inti=0;i<100;i++){
a=Integer.valueOf(a.intValue()+i);
}
}
我们不难发现,主要调用了两个方法,Integer.intValue()和Integer.valueOf(inti)方法
查看Integer源码,我们找到了对应代码:
/**
*Returnsthevalueofthis{@codeInteger}asan
*{@codeint}.
*/
publicintintValue(){
returnvalue;
}
/**
*Returnsan{@codeInteger}instancerepresentingthespecified
*{@codeint}value.Ifanew{@codeInteger}instanceisnot
*required,thismethodshouldgenerallybeusedinpreferenceto
*theconstructor{@link#Integer(int)},asthismethodislikely
*toyieldsignificantlybetterspaceandtimeperformanceby
*cachingfrequentlyrequestedvalues.
*
*Thismethodwillalwayscachevaluesintherange-128to127,
*inclusive,andmaycacheothervaluesoutsideofthisrange.
*
*@paramian{@codeint}value.
*@returnan{@codeInteger}instancerepresenting{@codei}.
*@since1.5
*/
publicstaticIntegervalueOf(inti){
if(i>=IntegerCache.low&&i<=IntegerCache.high)
returnIntegerCache.cache[i+(-IntegerCache.low)];
returnnewInteger(i);
}
很明显,我们我们得出,Java帮你隐藏了内部细节。
拆箱的过程就是通过Integer实体调用intValue()方法;
装箱的过程就是调用了Integer.valueOf(inti)方法,帮你直接new了一个Integer对象
那么哪些地方会进行自动拆装箱?
其实很简单
1.添加到集合中时,进行自动装箱
2.涉及到运算的时候,“加,减,乘,除”以及“比较equals,compareTo”,进行自动拆箱
注意的点
在上述的代码中,关于IntegervalueOf(inti)方法中有IntegerCache类,在自动装箱的过程中有个条件判断
if(i>=IntegerCache.low&&i<=IntegerCache.high)
结合注释
Thismethodwillalwayscachevaluesintherange-128to127,
inclusive,andmaycacheothervaluesoutsideofthisrange.
大意是:该方法总是缓存-128到127之间的值,同时针对超出这个范围的值也是可能缓存的。
那么为什么可能缓存?其实在IntegerCache源码中可以得到答案
StringintegerCacheHighPropValue=
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if(integerCacheHighPropValue!=null){
try{
inti=parseInt(integerCacheHighPropValue);
i=Math.max(i,127);
//MaximumarraysizeisInteger.MAX_VALUE
h=Math.min(i,Integer.MAX_VALUE-(-low)-1);
}catch(NumberFormatExceptionnfe){
//Ifthepropertycannotbeparsedintoanint,ignoreit.
}
}
因为缓存最大值是可以配置的。这样设计有一定好处,我们可以根据应用程序的实际情况灵活地调整来提高性能。
与之类似还有:
Byte与ByteCache,缓存值范围-128到127,固定不可配置
Short与ShortCache,缓存值范围-128到127,固定不可配置
Long与LongCache,缓存值范围-128到127,固定不可配置
Character与CharacterCache,缓存值范围0到127,固定不可配置
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。