Why do Java objects have to be a multiple of 8?

后端 未结 3 767
说谎
说谎 2020-12-09 09:20

I know that Java uses padding; objects have to be a multiple of 8 bytes. However, I dont see the purpose of it. What is it used for? What exactly is its main purpose?

3条回答
  •  生来不讨喜
    2020-12-09 10:17

    Data type sizes in Java are multiples of 8 bits (not bytes) because word sizes in most modern processors are multiples of 8-bits: 16-bits, 32-bits, 64-bits. In this way a field in an object can be made to fit ("aligned") in a word or words and waste as little space as possible, taking advantage of the underlying processor's instructions for operating on word-sized data.

提交回复
热议问题