开发者

How do I declare a 32bit integer in java?

开发者 https://www.devze.com 2023-01-23 10:02 出处:网络
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 w

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 ;)


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 (slightly paraphrased to make more readable, in my opinion):

The integral types are byte, short, int, and long, whose values are respectively 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, 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, ones' 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).


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


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.)


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


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


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?


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


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.


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

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


In java int is 32 bit. Longs are 64 bit. So if is is greater than 32 bit you need long.


Best way to do that it is import new libraries

import java.lang.Object;
import java.lang.Number;
import java.lang.Integer;
You should import these libraries, this will give you a new max values.
0

精彩评论

暂无评论...
验证码 换一张
取 消