Incremental Java
Primitive Types: Int

Int Types

When you buy a computer, one of the first things you should consider is the amount of RAM. Typical values for RAM is maybe 256 Megabytes of RAM.

We've been talking about boxes. Where are these boxes really located? To a first approximation, we can say these boxes are in RAM. The problem with RAM is that it's finite. 256 M of RAM is a lot, but it's not infinite.

Integers, on the other hand, can be infinitely long (or approach infinity, to be accurate). Thus, memory can never store arbitrary integers.

We must place limits on how big an integer.

This isn't so strange. Think about the car odometer. Most of them only allow up to 6 or 7 digits. For 6 digits, the range of mileage is from 0 up to 999,999. We can't have an infinite number of digits. We pick 6 or 7 because that seems like a reasonable value. Cars usually don't last to a million miles (some do, but it's rare). Thus, there's no need to make it 10 digits long or longer.

Similarly, we want to have enough digits (actually, bits, which store 0 or 1) so that we can store large numbers, but not so large that it becomes unwieldy to use.

For Java, int values use 32 bits. The range of values is from roughly negative 2 billion up to positive 2 billion. That may not seem like a very large range, but you'd be surprised how few computations that we'll work with get beyond a few thousand.

The key point to know about int is that it has a range. There is a maximum magnitude negative value and a maximum magnitude positive value.

(The exact range is -231 up to 231 - 1, inclusive, if you're really curious).

Int Literals

A literal is a written value. For example, 3 is an int literal. So is -3.

Literals can have a positive sign (though this is unnecessary) for positive values or zero, a negative sign for negative int values.

I'm sure you're used to writing numbers over 999 with a comma. For example, you might write 262,123,000.

In Java, you must leave the commas out, otherwise, that's an error. You write the literal as 262123000. I know that's harder to read, but you need to leave it out.