What is the storage cost for a boxed primitive in Java?

前端 未结 4 1848
醉酒成梦
醉酒成梦 2020-12-15 21:04

How large, in bytes, is a boxed primitive like java.lang.Integer or java.lang.Character in Java?

An int is 4 bytes, a typical

4条回答
  •  悲哀的现实
    2020-12-15 21:32

    One very small addition for these answers is that there is some deduplication happening for boxed primitives. For example, Integer::valueOf(int) uses java.lang.IntegerCache that uses Integer instances with values in range -128..127. So, you have above mentioned sizes for boxed objects, but not each of them would be a separate object.

提交回复
热议问题