You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
性能方面。see Integer#valueOf:as this method is likely to yield significantly better space and time performance by caching frequently requested values。
最后是为把啥缓存设置为[-128,127]区间:
技术规范。JLS7 5.1.7:If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p . It is always the case that a == b .
Java 基本类型的包装类的大部分都实现了常量池技术,即 Byte,Short,Integer,Long,Character,Boolean;这 5 种包装类默认创建了数值[-128,127] 的相应类型的缓存数据,但是超出此范围仍然会去创建新的对象。
如:
Integer one=333;
Integer two=333;
在创建one,是不是不会把对应的结果缓存起来啊?这才导致 one != two;
问:表面上时不会缓存的,那为什么不缓存呢?如果不缓存,为什么又要预先创建[-128,127] 呢?
The text was updated successfully, but these errors were encountered: