我知道這並不完全回答你的問題上盒裝元的存儲成本,但我從你的問題感覺到你在質疑是否不是你使用它們是有保證的。
這裏是由Joshua布洛赫從有效的Java(第2版)的摘錄,應該幫助你決定:
"So when should you use boxed primitives? They have several legitimate uses. The first is as elements, keys, and values in collections. You can’t put primitives in collections, so you’re forced to use boxed primitives. This is a special case of a more general one. You must use boxed primitives as type parameters in parame- terized types (Chapter 5), because the language does not permit you to use primi- tives. For example, you cannot declare a variable to be of type Thread- Local<int>, so you must use ThreadLocal<Integer> instead. Finally, you must use boxed primitives when making reflective method invocations (Item 53).
In summary, use primitives in preference to boxed primitives whenever you have the choice. Primitive types are simpler and faster. If you must use boxed primitives, be careful! Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the == operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program does unboxing, it can throw a NullPointerException. Finally, when your program boxes primitive values, it can result in costly and unnecessary object creations."
希望有所幫助。
從某個角度來看,這個問題是無法回答的,因爲在任何規範中沒有指定盒裝原語的開銷。它可以並且會因虛擬機和平臺而不同。具有標記內存的硬件平臺甚至可以具有零開銷。 – 2012-01-27 17:37:43
[在Java中,什麼是確定對象大小的最佳方法?](http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-確定最尺寸的-一個對象)。 – DwB 2012-01-27 17:52:03
我認爲它也可能取決於您使用盒裝int的上下文以及運行時編譯器正在進行的任何優化。 – 2012-01-27 17:54:03