Is there any reason to still use int as opposed to long on a 64 bit machine?

90 views Asked by At

In general, is there any reason to still use int as opposed to long in Java if programming on a 64 bit architecture?

Specifically in my case:

I use int variables that are being mapped to keys(sequential numbers starting from 1 and incremented for each new entry) of type NUMBER on an Oracle DB table.

The DB type NUMBER has way more precision then int so the correct way to model this on the Java side would be long.

I stuck to int for efficiency reasons and because probably we will never have so many entries in the table as to need more than an int can represent.

On the other hand, since most modern machines are 64 bit anyways, it shouldn't make a difference regarding efficiency on the Java side to use long as opposed to int, right? I guess it would make a difference memory wise if you load huge lists of keys into memory but we are not doing this kind of operation.

1

There are 1 answers

0
adao7000 On BEST ANSWER

On a 64-bit processor, long will give a marginal performance boost because of alignment, registers, whatever (whether or not this is actually palpable is debatable). This comes with a drawback of using more space on the stack and heap, which could itself impact performance negatively. Really, it depends on your program, since there are so many factors that could impact performance.

I would recommend to use int, unless you specifically need a 64-bit number. If you know for sure you're programming for an x86_64 architecture, then I think it wouldn't really matter, since these are optimized to handle 32-bit programs very efficiently anyways.