views:

287

answers:

9

I'm looking at an app in java that runs on both 32bit and 64bit systems, and it deals mainly with IP addresses. These IP addresses are kept as integers, and the normal type for this wastes a ton of memory on 64bit platforms (and yes, memory use has already been shown to be an issue here). How do you declare 32bit integral values that stay at 32bits even on x64 architectures?

As I'm a java novice, if there's a built-in type that's already designed to handle IP addresses please feel free to point that out to me ;)

+18  A: 

Java has specific widths for its data types, for portability. Integers are 32 bits wide even on 64-bit platforms. The Java language specification states quite clearly:

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units.

This is arguably due to the many portability issues discovered in C with the different widths (and even encodings like two's complement, one's complement and sign/magnitude) of integers that Java hoped to avoid, especially since it was meant to run on as many different platforms as possible.

As to a built-in type for handling IP addresses, Java has both Inet4Address and Inet6Address, depending on your needs (and their common ancestor, InetAddress).

paxdiablo
what about unsigned?
Joel Coehoorn
@Joel Java doesn't have unsigned
cobbal
+2  A: 

int primitive type of java have a fixed size of 32bit

pyCoder
what about unsigned?
Joel Coehoorn
+2  A: 

Use the primitive int type. It is 32 bit on all Java platforms; see JLS 4.2.1.

and yes, memory use has already been shown to be an issue here

Well, if you say so.

We don't know what you are doing here. But if you are concerned about the memory usage in representing huge numbers of IP addresses in memory, then maybe you need to:

  • not keep the IP addresses in memory,
  • increase your Java heap size, or
  • (if necessary) up-size your hardware or operating system.

Or maybe you've just gotten memory "wastage" out of proportion. Java is intrinsically memory hungry, and the pragmatic answer is to let it have what it wants.

FOLLOW UP

Java does not have an unsigned int type. But you don't need that to represent IP addresses, not least because you don't need to do arithmetic on them.

(And even if you did, there are ways to do it. Bear in mind that if you want to squeeze the last ounce of memory "wastage" out of a Java application, you are going to need to do some ugly things.)

Stephen C
what about unsigned?
Joel Coehoorn
A: 

From http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html :

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing Unicode characters.

So int is already 32-bit by definition, although the interpreter could be putting it on a 64-bit boundary in memory in order to speed up access.

Gintautas Miliauskas
A: 

"int" / "Integer" in Java is not platform dependent. It's always 32-bit. The platform dependent one is com.sun.jna.ptr.IntByReference.

vcsjones
A: 

In java ints are 32 bit already. Longs are 64 bit.

http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Slava Markeyev
+1  A: 

Don't worry, the int type in Java is defined (unlike in C/C++) to be exactly 32-bit, as mentioned here.

Zecc
+1  A: 

int is 32 bit on every platform. However, to make it unsigned, you have to manually shift numers and such stuff - it really gets complicated. Why not use a byte[] or a long?

thejh
+6  A: 

The int will be 32bits even on 64 bit machines, you don't need to do anything.

From the Java language spec (http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

The primary difference between 32bit and 64bit java VMs is the maximum heap size, i.e. the amount of memory the VM can manage.

However, the internal implementation of 64bit VMs will have an affect on memory usage:

Generally, the benefits of being able to address larger amounts of memory come with a small performance loss in 64-bit VMs versus running the same application on a 32-bit VM. This is due to the fact that every native pointer in the system takes up 8 bytes instead of 4. The loading of this extra data has an impact on memory usage which translates to slightly slower execution depending on how many pointers get loaded during the execution of your Java program.

From: http://www.oracle.com/technetwork/java/hotspotfaq-138619.html#64bit_description

Servicad