integer types

 

  There are five Java integer types. Their sizes and ranges are:

 

byte            8 bits                            -128 to +127

short          16 bits                          -32,768 to +32,767

int              32 bits                          -2 to the 31st to +2 to the 31st –1

long           64 bits                          -2 to the 63rd to +2 to the 63rd –1

char           16 unsigned bits            \u0000 to \uffff

 

  You cannot put double quotes around a char, as chars are always encased in single quotes. i.e.

 

char A = '\u00C1';  or  char c = '2';         works

char A = "\u00C1";  or  char c = "2";       won't compile

 

  If they are being incremented in a for loop, integers will simply run over their maximums and become negative.  No errors are generated.  i.e.

 

for ( byte b = 0; ;  b++) ;               will not stop at byte's maximum value of  127, or ever.

 

Such loops are difficult to code in actual programs because the compiler will detect the unreachable statements below them.

 

  Integer divide by zero in Java always results in an ArithmeticException being thrown. i.e.

 

int x = 5, y = 0, z;  

z = x / y;                    compiles but throws the exception.