byte to char to int..

and what is the result?!

This is the sample code I have..
class CastTest {
    public static void main(String... _) {
        final byte myByte = -1;
        final char myChar = (char) myByte;
        final int myInt = (int) myChar;
        System.out.println(myInt);
    }
}
Can you guess what the output will be?
myByte is represented as:
11111111
Then when widining to char, we fill the additional bits with 1, not 0, 1! A good explanation can be found here! Quoting..
When converting between the int, byte, short, char and boolean types the JVM needs to either add or truncate bits.
If the target type is represented by more bits than the type from which it is converted, then the JVM simply fills the additional slots with the value of the highest bit of the given value.
So because the highest bit we have is 1, just keep filling with 1, where myChar ends up as follows..
1111111111111111
Now we will further widen our value, from char (unsigned 16-bit) to int (signed 32-bit) but this time we will break the rule mentioned above, quoting again..
An exception from this rule is widening a char type which is, as we said before, unsigned. A conversion from a char is always applied by filling the additional bits with 0 because we said there is no sign and thus no need for an inverted notation.
So we end up with..
00000000000000001111111111111111
which equals... 65535!

Another mystery solved!