LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2007, 06:53 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
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
 
Old 04-27-2007, 09:29 AM   #2
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Rep: Reputation: 30
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.

Last edited by spaaarky21; 04-27-2007 at 09:38 AM.
 
Old 04-27-2007, 01:51 PM   #3
hollywoodb
Member
 
Registered: Aug 2003
Location: Minnesota, U.S.A.
Distribution: Debian, openSUSE
Posts: 400

Rep: Reputation: 30
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.
 
Old 04-27-2007, 04:02 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dual processor machine purchase question Goeland86 Linux - Hardware 1 01-18-2006 06:14 AM
sysinfo on 64bits machine with 32 bits kernels sarunya Linux - Newbie 0 08-29-2005 12:06 PM
Installing Linux on a dual processor machine (only one processor detected) rocordial Linux - Hardware 1 11-27-2004 02:16 AM
Mandrake 10.0 on a dual processor machine.. ommy Mandriva 8 06-17-2004 05:17 AM
Slackware installed on dual processor machine but only seeing 1... Simon W Linux - Software 2 04-16-2002 10:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration