LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   long in java with 32/64bits processor machine (https://www.linuxquestions.org/questions/programming-9/long-in-java-with-32-64bits-processor-machine-549448/)

xeon123 04-27-2007 06:53 AM

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
I get the error that int only has the size of 32 bit. (int, yeah, it's right). I don't understand why. Can anybody explain it?

Event if i do:
Code:

long value = (long)0xFFFFFFFFFFFF
I will get the same error.

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

spaaarky21 04-27-2007 09:29 AM

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.

hollywoodb 04-27-2007 01:51 PM

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.

jlliagre 04-27-2007 04:02 PM

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
{
        public static void main(String args[])
        {
                long l=0x7FFFFFFFFFFFFFFFL;
                System.out.println(l);
        }
}

Code:

$ java a
9223372036854775807

The second issue is you want to represent (2^64)-1 wich is impossible with 64 bit signed integers. You'd need 65 bit to do that ...


All times are GMT -5. The time now is 06:04 PM.