Primitive Types in Java

In the 32-bit version, the representable range is calculated by considering a sign bit, a 23-bit mantissa, and an 8-bit exponent with values between -126 and 127.

In the 32-bit version, the representable range is calculated by considering a sign bit, a 23-bit mantissa, and an 8-bit exponent with values between -126 and 127.

Furthermore, the standard provides for the representation of two values for zero (from right to left) two for infinity (positive and negative), and NaN (not a number) values to be used, for example, as results of impossible operations (e.g., divisions by zero).

Type Memory Quantity Represented Information Default Value
byte 8 bit Variable with sign (with "two's complement" representation, two's complement) and represents values in a range [-128 to 127] (inclusive) 0
short 16 bit Signed integers in a range [-32,768, 32,767] 0
int 32 bit Integers (by default signed, signed) in a range [-231, 231-1]. With Java 8, the possibility of using integers to represent unsigned quantities was introduced, which can have a range [0, 232-1] (thanks to specific static methods introduced in the Integer and Long classes) 0
long 64 bit Integers (by default signed, signed) in a range [-263, 263-1]. As with integers in Java 8, there is the possibility of using them as unsigned quantities with a range (positive) that reaches up to 264-1. 0L
float 32 bit Single-precision floating-point numbers according to the IEEE 754 specification, using sign, mantissa, and exponent representation.
(-1)sign * mantissa * 2exponent
0.0f
double 64 bit Double-precision floating-point numbers according to the IEEE 754 specification. The precision with which numbers are represented increases due to the increase in the number of bits used. 0.0d
boolean not specified, but a single bit would be sufficient serves to represent only 2 values: true or false (true or false). false
char 16 bit Used for storing characters from the Unicode character set) in the range ['', '￿'] (in hexadecimal) or equivalently [0,65535].

Exercise

/* I declare three types of variables to test their operation
*
* I perform the following exercise in the second instance to test the operation
* of the while loop
*
* a) I print the numbers from 1 to 10 and
* b) I print the numbers from 1 to 20 and when I reach 20, I continue printing in a
* decreasing manner from 20 to 1
* c) I print the odd numbers between 1 and 100
* d) I print the numbers from 1 to 100, highlighting the multiples of 25
* e) I print the numbers from 1 to 100 in a consecutive manner on a single line, highlighting the
* multiples of 100
* f) I print the numbers from 100 to 1, highlighting the multiples of 7
*
*
*/

public static void main(String[] args) {

// byte handles numbers from -128 to +127
// int char
byte a;
a=-128;
//System.out.println(a);
// we have tested that the number handled by byte
// ranges from -128 to +127

int b;
b=+2147483647;
// System.out.println(b);

char c;
c='@';
int contatore=0;
while(contatore<=1000)
{
System.out.print(contatore + " ");
System.out.print(c);
contatore++;
}

}

Online resources on the topic:

http://www.di-srv.unisa.it/professori/masucci/LPII-0405/slides/Lez03.pdf