首页 > 编程知识 正文

jmm面试题,interface和abstract区别 面试题

时间:2023-05-04 03:47:52 阅读:176251 作者:4924

Interger和Int之间的面试问题

public class test { publicstaticvoidmain (string [ ] args ) { Integer i1=100; Integer i2=100; Integer i3=200; Integer i4=200; system.out.println(I1==I2; system.out.println(I3==I4 ); }在上面的代码中,最后两行的输出语句分别是什么?

public class test { publicstaticvoidmain (string [ ] args ) { Integer i1=100; Integer i2=100; Integer i3=200; Integer i4=200; system.out.println(I1==I2; //true system.out.print ln (i3==i4 ); //false }}有些新人看到这样的答案会感到困惑,但内部的道理非常简单。

/* * returns an { @ code integer } instancerepresentingthespecified * { @ codeint } value.if anew { @ code integer } instance thed the constructor { @ link # integer (int ) } asthismethodislikely * toyieldsignificantlybetterspaceandtimeperformanceby * cachiely thismethodwillalwayscachevaluesintherange-128 to 127,* inclusive, andmaycacheothervaluesoutsideofthisrange.* * @ para mian { @ codeint } value.* @ return an { @ code integer } instancerereprepresesesese @ hotspotintrinsiccandidatepublicstaticintegervalueof (inti ) if (I=integer cache.lowi=integer cache.lowi return new intew }从integer的源代码来看,integer有一种叫做valueof(intI )的方法

上述注释显示,由于[-128~ 127]之间的数字频繁且广泛使用,因此在JDK5中,当值位于此区间时,java会去堆中调用,而不是直接去频繁的new。

无论是大于还是小于此区间,新对象都会被new,地址每次都不一样。 如果用==判断,自然会恢复为false

)在调试方式中,在此区间进行整形时,(如标题所示) i1和i2的地址相同,i3、i4则不同) )。

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