Why can't the compiler/JVM just make autoboxing “just work”?

前端 未结 6 1381
广开言路
广开言路 2020-12-31 07:40

Autoboxing is rather scary. While I fully understand the difference between == and .equals I can\'t but help have the follow bug the hell out of m

6条回答
  •  攒了一身酷
    2020-12-31 08:33

    A lot of people have problems with this issue, even people that write books about Java.

    In Pro Java Programming, mere inches below were the author talks about issues with using auto-boxed Integers as a key in an IdentityHashMap, he uses auto-boxed Integer keys in a WeakHashMap. The example values he uses are greater than 128, so his garbage collection call succeeds. If someone were to use his example and use values smaller than 128 though, his example would fail (due to the key being perma-cached).

提交回复
热议问题