|
|
|
Type name (keyword) | Number of bytes | Range of values |
---|---|---|
byte | 1 | −128 . . . 127 |
short | 2 | −32768 . . . 32767 |
int | 4 | −2,147,483,648 . . . 2,147,483,647 |
long | 8 | −9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807 |
|
|
public class Safe { public static void main(String[] args) { int x; short y; y = 1; x = y; // Safe conversion System.out.println(x); } } |
What happens inside the computer (behind the scene):
|
public class UnSafe { public static void main(String[] args) { int x; // x uses 4 bytes short y; // y uses 2 bytes x = 65538; // 65538 = 2^16 + 2 y = (short) x; // Unsafe conversion System.out.println(y); //***** Prints 2 !!! // Should be: 65538 !!! } } |
What happens inside the computer (behind the scene):
|
How to run the program:
|