Answer the question
In order to leave comments, you need to log in
So how much does int and long actually take in RAM in Java?
It's clear that int and long are 32 and 64 bits, respectively. But somewhere I heard that both byte and short and int and long occupy the same amount of memory in memory, and the difference in them is the limitation of the value. Is it true? Google didn't help.
Answer the question
In order to leave comments, you need to log in
Nothing on the stack takes up less than 4 bytes, because the bytecode only has instructions for operations on int, long, float, double and object references on the stack: https://en.wikipedia.org/wiki/Java_bytecode_instru...
In object memory, all types occupy the "necessary" number of bytes (with the exception of boolean, which could take only one bit, but takes a whole byte, see java.util.BitSet), but due to object alignment (object sizes are aligned by default (and at least) on the boundary of 8 bytes in Hotspot JVM) it often turns out that one byte or boolean field increases the object size by 4-8 bytes at once. (But adding the next 3-7 fields will be kind of "free").
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question