Type |
Description |
Size |
---|---|---|
int |
The integer type, with range -2,147,483,648 . . . 2,147,483,647 | 4 bytes |
byte |
The type describing a single
byte, with range -128 . . . 127 |
1 byte |
short |
The short integer type, with
range -32768 . . . 32767 |
2 bytes |
long |
The long integer type, with range -9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807 | 8 bytes |
double |
The double-precision
floating-point type, with a range of about ±10308 and
about 15 significant decimal digits |
8 bytes |
float |
The single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits | 4 bytes |
char |
The character type, representing
code units in the Unicode encoding scheme |
2 bytes |
boolean |
The type with the two truth
values false and true |
1 bit |
![]() |
And so on...
In other words, these relationships also hold:
Here is an example to illustrate conversion:
public class Long2Int { public static void main(String[] args) { int i; long l; l = 2147483647; i = l; // WARNING: Can't be expressed like this in Java. // I only write it this way to express an intension: // l contains a long integer value // i can contain an int value // A "long" is different from "int", but similar // enough to be converted to each other } } |
i = l;is taking a long value stored in the (type long) variable l, and trying to store it in an type int variable i
The above assignment operation is unaccaptable in Java, because long and int are different. |
A conversion is called a cast.
(typeName) expression |
converts the value of the expression to the type typeName
int i; long l; l = 2147483647; i = (int) l; |
i = (int) l: takes (long) value in l and converts it into an int value. Then the result is assigned to the int variable i.
int i; double d; d = 827.3984; i = (int) d; |
i = (int) d: takes (double) value in d and converts it into an int value. Then the result is assigned to the int variable i.
NOTE: when casting a floating point value into an integer value, Java will TRUNCATE: the floating point value !!! (Does NOT round up !)
int i; long l; l = 2147483647; i = (int) l; |
Answer:
The value stored in l can be one of these values: -9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807.
The value that can be stored in i must be one of these values: -2,147,483,648 . . . 2,147,483,647
Because the range of values that l can hold is larger (exceeds) the range of values that i can hold, we CANNOT be certain that i can hold the value that is currently stored in l....
Answer:
int i; double d; d = 827.3984; i = (int) d; |
public class Int2Long { public static void main(String[] args) { int i; long l; i = 2147483647; l = (long) i; // i can contain an int value // l contains a long integer value // A "int" is different from "int", but // similar enough to be converted to // each other } } |
Answer:
The value stored in i can be one of these values: -2,147,483,648 . . . 2,147,483,647
The value that can be stored in l can be one of these values: -9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807.
Because the range of values that l can hold is cotains the range of values that i can hold, we can be certain that l can hold the whatever value that is currently stored in i....
Answer:
The int to long conversion is safe !!!
![]() |
When a conversion is
safe,
the cast operation
is not required
Java will perform a safe conversion automatically without the programmer having to specify a cast operation) |
public class Int2Long { public static void main(String[] args) { int i; long l; i = 2147483647; l = i; // i can contain an int value // l contains a long integer value // int -> long conversion is safe // We do not need to specify the // cast operation (long) } } |
![]() |
int i; long l; l = 2147483647; i = (int) l; |
i = (int) l: takes (long) value in l and converts it into an int value. Then the result is assigned to the int variable i.
int i; double d; d = 827.3984; i = (int) d; |
i = (int) d: takes (double) value in d and converts it into an int value. Then the result is assigned to the int variable i.
For instance, multiply goes before addition.
4 + 4 * 4 = 4 + (4 * 4) = 4 * (16) = 20 |
3 + (int) 4.7 * 5 = 3 + ((int) 4.7) * 5 = 3 + (4) * 5 = 3 + (4 * 5) = 3 + (20) = 23 |