So, I was trying to do a for loop earlier like this:
and I kept getting: integer number too large and I realized something... How the heck can a long be 64 bits on a 32 bit machine?? I mean, in every Java book I have been reading, I haven't noticed them state anything about that. If they have, I must of skimmed over it... Which is HIGHLY possible. However, I am curious... Is the long a pseudo 64 bit integer somehow? Or am I right in thinking that if I'm sitting on a 32 bit Intel machine, then the long can only be 32 bits and hence my compiler error? Thanks!
Hi Dean, Welcome to JavaRanch! First, the syntax error: you have to write "long" constants as 50000000000000L -- that trailing "L" means it's a long, not an int. Now, how can longs be 64 bits? The JVM specification explains that they take up two 32-bit slots on the JVM stack, or two adjacent 32-bit registers. This has some interesting consequences: for example, reading or writing to long values is not "atomic" with respect to multithreading. You have to be careful with longs or you can accidentally "see" partially-written values. In a "real" 32-bit computer, it works much the same way (although there aren't rigid stack "slots", the registers are indeed a fixed 32 bits.) There are usually special CPU instructions that operate on longs; the register-based versions assume that multiple registers are being used to hold the value. The threading issues naturally fall out of this architecture.