What is the output of the following code?
SYSTEM.OUT.PRINTLN ((int) (char) (byte) (-1));
3 transformations have been made here, and the results are not visible at once.
First time, -1,int
The second time,?, byte: Here do the truncation 0xFFFFFFFF turns into 0xFF.
Third time, huh? , Char: Here char is unsigned, Byte signed. When extended, the symbol bit is taken and the result is 0xffff.
Fourth time, huh? , int: Here an int with a symbol, but char is unsigned. The extension does not have a sign bit, and the result is 0xffff.
So the output is 65535,0xffff.
Summarize
When a numeric conversion occurs, "truncation" and "extension" occur.
- Truncation is relatively simple: keep low
- The extension is judged according to the current type: If it is signed, such as Byte, the extension needs to expand the symbol bit. If no symbols, such as char, are extended high position 0
In actual development, it needs to be handled according to its own requirements, such as adding Masks & 0xFF. Ensure that the results meet your own requirements.
Java Essay: Multiple transformations