Understanding byte, short, int and long types
Different types of integer data types with its maximum and minimum value limits are explained with this code.
- Declaration and initialisation of byte, short, int and long types.
- Limits of values
- Literal depiction of all integer types.
byte data type maximum value is assigned here
byte data type (8 bits) minimum value is assigned here. This is the lowest negative number in 8 bits. The reason that this is 1 digit bigger is that 0 is considered on positive side and sign bit is not on in that case. So the the total count with 8 bits has to be 256 which is (128 for negative values, 1 for 0 and 127 for positive values)
short data type (16 Bits) maximum value is assigned here.
short data type (16 bits) minimum value is assigned here. This is the lowest negative number in 16 bits.
int data type (32 Bits) maximum value is assigned here.
int data type (32 bits) minimum value is assigned here. This is the lowest negative number in 32 bits.
long data type (64 Bits) maximum value is assigned here. Notice the use of L suffix. Without this suffix you will not be able to store this literal in 64 bits as the system without suffix defaults to 32 bits only.
long data type (64 bits) minimum value is assigned here. This is the lowest negative number in 64 bits.
- Please note that all storage is always in binary form and the most significant bit is always used in Java to store the sign bit. 0 for positive values and 1 for negative values. Java does not have the unsigned version of integer values. So the largest value is always the 2^(no of bits)/2 -1 . For e.g. for 16 bit it would be (2^16)/2 -1 = 32767. One can also write this as 2^(no of bits -1) – 1.
Suggested Filename(s): IntTypes.java
sunmitra| Created: 19-Mar-2018 | Updated: 19-Mar-2018|