首页 > 编程知识 正文

java类直接赋值,java数组循环赋值

时间:2023-05-05 05:14:02 阅读:176179 作者:3569

Java Interger赋值问题package anomyous; public class test { publicstaticvoidmain (string [ ] args ) integerI1=newinteger ) 99; integerI2=integer.valueof(99; //手动装箱Integer i3=99; //自动装箱隐式调用integer.value of (system.out.println ) I1==I2 )//false system.out.println (I2==i3 ); //true是Integer.valueOf ) )来创建对象。 //优先保存内存中是否存在其他同类对象,如果存在,则保存此值并指向该对象的地址。 //这与String相同,所以i2和i3的地址相同。 system.out.println ) ) string//false system.out.println (' end ' ); }补货:开箱操作

//开箱int i4=i1.intValue (; int i5=i2.intValue (; int i6=i3.intValue (;

package anomyous; public class test { publicstaticvoidmain (string [ ] args ) integerI1=newinteger ) 99; integerI2=integer.valueof(99; Integer i3=99; system.out.println(I1==I2; //false system.out.print ln (I2==i3 ); //true system.out.print ln (i1==i3 ); //false i2=100; //已重新创建目标system.out.println(I3 ); //99system.out.println(I2==I3 ); 如果//false的值发生变化,i2又会指向另一个地址,从而重新创建对象。 //此时,i2和i3的地址不相同,这也与String相似。 system.out.println('end ); }接下来可以看出和String的不同

package anomyous; public class test { publicstaticvoidmain (string [ ] args )//这次尝试增大初始值的integerI1=newinteger ) 99 ); integerI2=integer.valueof(999; Integer i3=999; system.out.println(I1==I2; //false system.out.print ln (I2==i3 ); //false以前说过,Integer.valueOf创建对象后,会检查内存中的其他同类对象是否保存了此值。 //此时不超过阈值寻找其他现有对象。 那么,这个阈值是多少呢,从源代码中可以明确。 system.out.println (i1==i3//false system.out.println (' end ' ); }源代码

首先来看看valueOf方法的源代码

//thismethodwillalwayscachevaluesintherange-128 to 127, 注意上述措辞,并注意到publicstaticintegervalueof (inti ) if ) I=integer.integer cache.lowi=integer.integer cache.high )/low和returnnewinteger(I; 显示智能缓存源代码

privatestaticclassintegercache { staticfinalintlow=-128; //这里是最小值static final int high; static final Integer cache[]; //静态代码块的执行优先级高于非静态初始化块,随着类的加载而执行,static {//highvaluemaybeconfiguredbypropertyinth=127; //此处为stringintegercachehighpropvalue=VM.getsavedproperty (' Java.lang.integer.integer cache.high ' ); 集成缓存高性能!=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=newinteger[(high-low )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; } private IntegerCache () )目前已知此范围为[-128,127 ]

接下来,通过实例验证上述推测

package anomyous; public class test { publicstaticvoidmain (string [ ] args ) integerI2=integer.valueof ) 128; Integer i3=128; system.out.println(I2==I3 ); //128时falseintegeri4=integer.value of (127; Integer i5=127; system.out.println(I4==I5 ); 如果为//127,则true 127返回阈值integerI6=integer.valueof(-129 ); Integer i7=-129; system.out.println(I6==I7; //-129时falseintegeri8=integer.value of (-128; Integer i9=-128; system.out.println(I8==I9 ); 如果为//-128,则true -128为阈值system.out.println('end ); }

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