在《effectivejava》中‘避免创建不必要的对象’一节,有如下代码privatestaticlongsum(){Longsum=0L;for(longi=0;i<=Integer.MAX_VALUE;i++)sum+=i;returnsum;}Thisprogramgetstherightanswer,butitismuchslowerthanitshouldbe,duetoaone-charactertypographicalerror.ThevariablesumisdeclaredasaLonginsteadofalong,whichmeansthattheprogramconstructsabout231unnecessaryLonginstances(roughlyoneforeachtimethelongiisaddedtotheLongsum).为什么把sum设置成Long对象,会在每一次循环的时候创建Long实例?
慕婉清6462132
沧海一幻觉
芜湖不芜
随时随地看视频慕课网APP
相关分类