R
R
Rienopely2015-10-02 11:27:29
Java
Rienopely, 2015-10-02 11:27:29

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

3 answer(s)
L
leventov, 2015-10-03
@rienopely

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").

S
Saboteur, 2015-10-02
@saboteur_kiev

https://en.wikibooks.org/wiki/Java/%D0%A2%D0%B8%D0...

E
Evhen, 2015-10-02
@EugeneP2

"Google didn't help" - well, well...
https://www.google.com.ua/webhp?sourceid=chrome-in...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question