16.2: Integer Data Type
-
- Last updated
- Save as PDF
Overview
An integer data type represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits (bits). The size of the grouping varies so the set of integer sizes available varies between different types of computers and different programming languages. [1]
Discussion
The integer data type basically represents whole numbers (no fractional parts). The integer values jump from one value to another. There is nothing between 6 and 7. It could be asked why not make all your numbers floating point which allow for fractional parts. The reason is threefold. First, some things in the real world are not fractional. A dog, even with only 3 legs, is still one (1) dog not ¾ of a dog. Second, the integer data type is often used to control program flow by counting, thus the need for a data type that jumps from one value to another. Third, integer processing is significantly faster within the CPU than is floating point processing.
The integer data type has similar attributes and acts or behaves similarly in all programming languages that support it.
| Language | Reserved Word | Size | Range |
|---|---|---|---|
| C++ |
short
|
16 bits / 2 bytes | -32,768 to32,767 |
| C++ |
int
|
varies | depends on compiler |
| C++ |
long
|
32 bits / 4 bytes | -2,147,483,648 to 2, 147,483,647 |
| C++ |
long long
|
64 bits / 8 bytes | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| C# |
short
|
16 bits / 2 bytes | -32,768 to32,767 |
| C# |
int
|
32 bits / 4 bytes | -2,147,483,648 to 2, 147,483,647 |
| C# |
long
|
64 bits / 8 bytes | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| Java |
short
|
16 bits / 2 bytes | -32,768 to32,767 |
| Java |
int
|
32 bits / 4 bytes | -2,147,483,648 to 2, 147,483,647 |
| Java |
long
|
64 bits / 8 bytes | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| JavaScript | N/A | ||
| Python |
int()
|
no limit | |
| Swift |
Int
|
varies | depends on platform |
| Swift |
Int32
|
32 bits / 4 bytes | -2,147,483,648 to 2, 147,483,647 |
| Swift |
Int64
|
64 bits / 8 bytes | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
For C++ and Swift the size of a default integer varies with the compiler being used and the computer. This effect is known as being machine dependent . These variations of the integer data type are an annoyance for a beginning programmer. For a beginning programmer, it is more important to understand the general attributes of the integer data type that apply to most programming languages.
JavaScript does not support an integer data type, but the
Math.round()
function may be used to return the value of a number rounded to the nearest integer.
[2]
Python 3 integers are not limited in size, however,
sys.maxsize
may be used to determine the maximum practical size of a list or string index.
[3]
Key Terms
- machine dependent
- An attribute of a programming language that changes depending on the computer’s CPU.