Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type casting in Java from char to short [duplicate]

Tags:

java

casting

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.

like image 880
Adarsh kumar yadav Avatar asked Jul 27 '17 13:07

Adarsh kumar yadav


People also ask

Can we type cast char to short?

Short has a minimum value of -32,768 and a maximum value of 32,767. Integer has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. So char can fit into integer but not into short, that's why you should assure java that you want to do typecast here.

Can you cast a char to a string Java?

We can convert a char to a string object in java by using the Character. toString() method.

What is the problem with type casting in Java?

In the case of Narrowing Type Casting, the higher data types (having larger size) are converted into lower data types (having smaller size). Hence there is the loss of data. This is why this type of conversion does not happen automatically. Note: This is also known as Explicit Type Casting.


2 Answers

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.

like image 196
Jon Skeet Avatar answered Sep 20 '22 13:09

Jon Skeet


Java specification says:

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

Char has a minimum value of 0 and a maximum value of 65,535.

Short has a minimum value of -32,768 and a maximum value of 32,767.

Integer has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647.

So char can fit into integer but not into short, that's why you should assure java that you want to do typecast here.

like image 37
Max Avatar answered Sep 18 '22 13:09

Max