-1

The Java Language specification (Section 3.10.1) states the following:

An integer literal is of type long if it is suffixed with an ASCII letter L or l [...]; otherwise it is of type int (ยง4.2.1).

So far so good, this means that the following two assignments will result in different results.

long x = 2_000_000_000 * 40_000;
long y = 2_000_000_000 * 40_000L;

So I see why the idea of having standardized Literal-Types makes sense, and why it's needed, but I can't really see the benefit of choosing int over long as the standard type for literals. After all, long would enable a broader amount of operations without the extra need to add specific suffixes as far as I can see.

If someone could explain the advantage to me, that would be of great help! Thanks a lot in advance!

Vlad
  • 18,195
  • 4
  • 41
  • 71
Benjamin
  • 99
  • 8

1 Answers1

1

long requires double the amount of memory required to store an int. Keep in mind that Java runs on a lot of different devices, including memory-constrained embedded systems, mobile phones etc.

On a 32-bit machine, working with longs might be slower. Adding together two ints can be one CPU instruction, whereas adding two longs might require several.

Vlad
  • 18,195
  • 4
  • 41
  • 71