Computers store data as sequences of binary digits that can be interpreted in many different ways. Programming languages such as Java can interpret data as a numerical value. Java contains many data types that represent integers, called the integral types. There are many integral types, each of which can represent a different range of numbers.
The various integral types have limitations on what numbers they can represent. This is because they have a fixed size in memory. The integral types and their ranges are listed below:
byte: -128 to 127 short: -32768 to 32767 int: -2147483648 to 2147483647 long: -9223372036854775808 to 9223372036854775807 char: 0 to 65535
The integral types support a variety of arithmetic operators, allowing you to perform basic mathematical computations with them. In addition to arithmetic operators, integral types support comparison operators such as less than, greater than, and equal to. They also support bitwise operations, which act on the underlying binary data.
When an integer exceeds it maximum or minimum value, it either overflows or underflows. For example, if one is added to a byte of value 127, it does not equal 128, but instead overflows. This must be planned for by the programmer to avoid inaccurate data and program crashes.
When an arithmetic operation takes place involving one long type and any other integral type, the other type is promoted to a long. Promotion means that the variable type is changed to long, and its range of possible values increases to match the long type. A long integral type requires 64 bits of computer memory to store a value, and operations involving longs require 64-bit precision. This means that all numbers involved in the operation are 64-bits wide. This is significant, because all other arithmetic operations use 32-bit precision. On certain computer architectures, it is faster to perform 32-bit precision operations than 64-bit. By using long types when absolutely necessary, you can potentially increase the performance of your Java application.