2

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?

gd1
  • 11,300
  • 7
  • 49
  • 88
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
  • 1
    Please clarify your questions; **1** It is a virtual machine. **2** Yes. Serialize it to a `ByteArrayOutputStream` and get the length of the array when you're done... but why? – Elliott Frisch Jan 07 '14 at 21:10
  • possible duplicate of [Calculate size of Object in Java](http://stackoverflow.com/questions/9368764/calculate-size-of-object-in-java) – erickson Jan 07 '14 at 21:12
  • http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – erickson Jan 07 '14 at 21:12
  • "The external address and data buses are often wider than 32 bits but both of these are stored and manipulated internally in the processor as 32-bit quantities." http://en.wikipedia.org/wiki/32-bit – WonderWorld Jan 07 '14 at 21:15

2 Answers2

4

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

Community
  • 1
  • 1
gd1
  • 11,300
  • 7
  • 49
  • 88
2

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • can you explain how long which is 64 bit is stored in 32-bit processor – eagertoLearn Jan 07 '14 at 21:37
  • As two 32 bits words, and if the processor has no 64 addition, two 32 bit additions are done. Java byte code is primarily interpreted, and with a just-in-time hotspot compiler translated to machine code. As almost any full fledged programming language it can do all. There are JVMs for 16 bit processors (maybe no more). – Joop Eggen Jan 07 '14 at 21:40