long in java with 32/64bits processor machine
Hi,
1 - In a machine with 64bits processor, the long will have a size of 64 bits? 2 - And, with a 32bit processor, the long will have the same size? 3 - I'm building a java class that uses a long, but if i do: Code:
long value = 0xFFFFFFFFFFFF Event if i do: Code:
long value = (long)0xFFFFFFFFFFFF 4 - How can i represent a long with a value of (2^64) - 1 in java? 5 - If i'm building a program that uses long values, should i pay attention if the code is going to run on machines with 64/32 bits processors? 6 - And, if i have a machine with multiprocessors of 32bits, can i run the program safely? Thanks, Pedro |
Even in languages that run "directly on the processor" like C++, the size of a variable is compiler-specific, not processor-specific. In Java, the size of variables is part of the Java standard and will never change.
Even IF variable size depended on the platform your code ran on, it wouldn't matter because Java code always runs on the same machine - a Java Virtual Machine that is essentially being emulated on your local machine. I'm a little rusty at hex so I don't know that I could answer all your questions off the top of my head but I hope that helped at least a little. |
Yep. Since Java apps run under the JRE, the actual hardware the JRE is installed on is insignificant. This is actually a "good" thing for Java, since one Java application will run in the same manner if using the same version of the JRE on any hardware. This is also why Java apps don't need to be compiled for different architectures.
|
In java, data types have a defined size regardless of the underlying architecture, so you have 32 bit signed ints and 64 bit signed longs.
The problem is you are missing a L suffix in your program to tell the compiler the value is a long, not an int. Here is a sample code that demonstrates how to display (2^63)-1, the largest long integer usable with Java. Code:
public class a Code:
$ java a |
All times are GMT -5. The time now is 06:04 PM. |