The integer data types are used to store whole numbers like 2, 3, 267, -1245, 0, 8382923 etc. Integers do not contain decimal points, so 3.14 and 4221.76 are not valid integers. Java supports four integer types depending upon the sizes -
The four integer types in the order of size are:
- Depending upon the requirement we should choose the appropriate data type
3-min video about integer data types
- The size allowed is calculated using the number of bytes. For eg for byte the size is 8-bits, so the range allowed is 2 power 8 which is 256. This 256 is split into half to support both negative and positive numbers. That is reason the size is -128 to +127. The positive side is +127 instead of +128, because zero is also included in the range.