411 REFERENCE · Wikibooks
Programming Fundamentals/Integer Data Type
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
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.Wikipedia: Integer (computer science)
Discussion
The integer data type 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.
LanguageReserved WordSizeRangeC++short16 bits / 2 bytes -32,768 to 32,767C++intvariesdepends on compilerC++long32 bits / 4 bytes -2,147,483,648 to 2, 147,483,647C++long long64 bits / 8 bytes−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807C#short16 bits / 2 bytes -32,768 to32,767C#int32 bits / 4 bytes -2,147,483,648 to 2, 147,483,647C#long64 bits / 8 bytes−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Javashort16 bits / 2 bytes -32,768 to32,767Javaint32 bits / 4 bytes -2,147,483,648 to 2, 147,483,647Javalong64 bits / 8 bytes−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807JavaScriptN/APythonint()no limitSwiftIntvariesdepends on platformSwiftInt3232 bits / 4 bytes -2,147,483,648 to 2, 147,483,647SwiftInt6464 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.Mozilla: Math.round()
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.Python.org: Integers
Key Terms
machine dependent
An attribute of a programming language that changes depending on the computer’s CPU.
References
cnx.org: Programming Fundamentals – A Modular Structured Approach using C++