Type casting in Java from char to short

Why can we not pass a char value as an argument to any method that accepts a short parameter, whereas we can pass a char value to another method whose parameter is of int type? Why does type casting not take place from char to short, given that the sizes are equal? I hope short can also store as much values as short can.

Jon Skeet
people
quotationmark

Why can we not pass a char value as a argument to any method that accepts a short parameter

Because there's no implicit conversion from char to short in an invocation context.

whereas we can pass a char value to another method whose parameter is of int type?

That's because there is an implicit conversion available from char to int in an invocation context.

Why does type casting not take place from char to short, given that the sizes are equal? I hope short can also store as much values as short can.

Although char and short are the same size, char is unsigned whereas short is signed. That's why there's no implicit conversion from char to short.

The conversion from char toint is a widening primitive conversion (JLS 5.1.2) whereas the conversion from char to short is a narrowing primitive conversion (JLS 5.1.3). In particular (emphasis mine):

A narrowing conversion of a char to an integral type T likewise simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the resulting value to be a negative number, even though chars represent 16-bit unsigned integer values.

people

See more on this question at Stackoverflow