Understanding byte, short, int and long types – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3846    siteicon   siteicon  

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.

Learning Objectives

  • Declaration and initialisation of byte, short, int and long types.
  • Limits of values
  • Literal depiction of all integer types.

Source Code

TC++ #3846

Source Code

class IntTypes
{
public static void main(String[] args)
{
byte bp=127;
byte bn=-128;
short sp=32767;
short sn=-32768;
int ip=2147483647;
int in=-2147483648;
long lp=9223372036854775807L;
long ln=-9223372036854775808L;
System.out.println("Largest byte type value = "+bp);
System.out.println("Smallest byte type value = "+bn);
System.out.println("Largest short type value = "+sp);;
System.out.println("Smallest short type value = "+sn);;
System.out.println("Largest int type value = "+ip);;
System.out.println("Smallest int type value = "+in);;
System.out.println("Largest long type value = "+lp);
System.out.println("Smallest long type value = "+ln);;
}
}

Test it !

Run Output

Largest byte type value = 127
Smallest byte type value = -128
Largest short type value = 32767
Smallest short type value = -32768
Largest int type value = 2147483647
Smallest int type value = -2147483648
Largest long type value = 9223372036854775807
Smallest long type value = -9223372036854775808

Code Understanding

byte bp=127;
byte data type maximum value is assigned here

byte bn=-128;
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 sp=32767;
short data type (16 Bits) maximum value is assigned here.

short sn=-32768;
short data type (16 bits) minimum value is assigned here. This is the lowest negative number in 16 bits.

int ip=2147483647;
int data type (32 Bits) maximum value is assigned here.

int in=-2147483648;
int data type (32 bits) minimum value is assigned here. This is the lowest negative number in 32 bits.

long lp=9223372036854775807L;
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 ln=-9223372036854775808L;
long data type (64 bits) minimum value is assigned here. This is the lowest negative number in 64 bits.

Notes

  • 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



Share

sunmitra| Created: 19-Mar-2018 | Updated: 19-Mar-2018|






Back