Skip to content
AgileLogics

Programming glossary

Integer

A whole number, with no decimal point, such as 42 or -7.

Counting is where integers shine. The number of items in a basket, a person's age in years, the floor of a building, the position of an array element: none of these ever needs a fractional part. Integers can be positive, negative or zero, and arithmetic on them is exact, which is not always true of numbers with decimals.

Example

int apples = 17;
int people = 5;

System.out.println(apples / people); // 3   (division drops the remainder)
System.out.println(apples % people); // 2   (the remainder)
System.out.println(apples / 5.0);    // 3.4 (a decimal operand gives a double)

int big = Integer.MAX_VALUE;          // 2147483647
System.out.println(big + 1);          // -2147483648 (overflow wraps around)
long safer = 3_000_000_000L;          // long holds much larger values

Watch for that first line in other languages too. Python 3 gives 3.4 for 17 / 5 and needs // for whole-number division.

How big can an integer be?

That depends on the language. Java's int is 32 bits, covering roughly minus 2.1 billion to plus 2.1 billion, and exceeding the limit silently wraps around as shown above; long uses 64 bits for bigger values. PHP integers are normally 64-bit, and a result that overflows becomes a float. Python is the generous one: its int grows to whatever size memory allows, so 2 ** 200 comes back exact.

JavaScript is the odd case. It has no separate integer type; ordinary numbers are 64-bit floating-point values, which represent whole numbers exactly only up to 2^53 - 1 (Number.MAX_SAFE_INTEGER). Beyond that there is BigInt, written with an n suffix like 9007199254740993n.

Integer vs float

A float (or double) stores numbers with a fractional part, such as 3.14, using an approximate binary format. That is why 0.1 + 0.2 prints 0.30000000000000004 in many languages. Use integers for counts and IDs, and avoid floats for money; storing amounts as whole cents is a common fix.

Related tools

Last updated