Search code examples
javaobjectlong-integerprimitivebit

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


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?


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