How are long integers processed in 64 bit computers? How can I determine the size of an object? [closed]

StackOverflow https://stackoverflow.com/questions/20981919

Question

Q1: If my computer has 32-bit processor, how does long type which is 64-bit be stored and used for processing? On the other hand, if I am using only int types in my 64-bit processor, am I not wasting 32-bits of memory?

Q2: For primitives, we say byte is 8-bit, int is 32-bit. How do we say the same for objects? Is there a way to get the size of objects in Java. or what is the maximum size an object can be?

Was it helpful?

Solution

For the second question, please see this.

For the first question, 32-bit processors cannot directly deal with 64-bit integers as 64-bit operands cannot be stored in 32-bit registers "as a whole". Compilers solve this issue by transforming 64-bit operations into a series of operations on 32-bit operands which represent halves of 64-bit operands. See this answer for a thorough explanation.

In Java, long is guaranteed to be exactly 64 bits, int is guaranteed to be exactly 32 bits. Using 64-bit integers on a 32-bit machine probably impacts performance, but given the nature of Java I don't think this would be pretty much noticeable (maybe in number crunching applications).

OTHER TIPS

Java has fixed sized data type, intentionally. Where in C int may vary in size, with java int always is signed 32 bits. So the remaining questions concern the implementation.

And that may vary. But looking at the JVM byte code instructions there are some decisions made. A local variable of type byte may be stored in an int (32 bits). And so on. Also Oracle 64 bits java has an option to use not 64 bits for object references but 32 bits, saving hugely in memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top