首页 > 编程知识 正文

integer数组转int数组,javaintinteger例子

时间:2023-05-05 22:13:47 阅读:146721 作者:3772

Integer和int的==关于比较:对于int基本类型,==比较的是值

对于整数类型,比较内存地址并使用相同的引用。

新新一个包装类,地址变了,也一定不相等;

注意备注publicstaticvoidmain (字符串[ ] args ) { Integer a=100; int a1=100; system.out.println(a==a1 ) ) a==a1 ); integerb=integer.valueof(200; Integer bb=200; system.out.println(b==bb ) ) b==bb ); //new以外输出的Integer如果是-128到127的数,则true int b2=200; system.out.println(b==B2 ) ) b==B2 ); //Integer和int==进行比较时,无论是直接代入还是new制作的对象,与int进行比较的话都会开箱为int型,因此是相等的。 integerc=newinteger(100; 系统. out.println (a==c ) ) a==c ); //Integer和new Integer不相等。 内存的存储位置不同,因此不进行开箱处理。 system.out.println(a1==c ) ) a1==c ); }

128陷阱:小于127大于-128时,自动装箱为同一地址存在的数值。 大于127时,自动装箱不在同一地址,因此==比较结果为false

valueOf )查看源代码:从源代码中可以看到,-128-127之间的所有数值都存储在一个catch数组中,在-128-127之间自动装箱后,该值会直接返回到内存中的同一地址,并返回

publicstaticintegervalueof (inti ) if ) I=integer cache.lowi=integer cache.high ) returnintegercache.cache(I(-) }

privatestaticclassintegercache { staticfinalintlow=-128; 静态金融入高; 静态final integer cache [ ]; 静态{//highvaluemaybeconfiguredbypropertyinth=127; stringintegercachehighpropvalue=sun.misc.VM.getsavedproperty (' Java.lang.integer.integer cache.high ' ); Integercachehighpropvalue!=null } { try { inti=parseint (integercachehighpropvalue ); I=math.max(I,127 ); //maximumarraysizeisinteger.max _ valueh=math.min (I,integer.max_value-(-low )-1 ); } catch (numberformatexceptionnfe )/ifthepropertycannotbeparsedintoanint,ignore it. } } high=h; cache=new integer [ (高-低)1]; int j=low; for(intk=0; k cache.length; k ) cache[k]=newinteger(j; //range [-128,127 ] must be interned (jls 75.1.7 ) assert IntegerCache.high=127; }私有integercache { } { }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。