Question:
double m = (byte) 110_987_654_6299.123_34;
Answer:
To be fair, the result (the value of m
) will be -1.0
, not -1
.
3 actions take place:
-
double
number110_987_654_6299.123_34
is cast toint
. The result will be2147483647
-
int
number2147483647
is cast tobyte
. The result will be-1
-
byte
number-1
is cast todouble
. The result will be-1.0
Why the cast works this way – you can read in the documentation . In short, then:
- when casting
double
tobyte
(the right side of the expression),double
is first cast toint
, and only then –int
tobyte
- casting
double
toint
in case of exceeding the allowedint
value returns the maximum value ofint
- casting
int
tobyte
just takes the last 8 bits fromint