IntelliJ IDEA complains about this code:
char c = 'A';
if (c == 'B') return;
The warning is on the second line:
Implicit numeric conversion from char to int
What does it mean? What does it expect me to do?
The explanation for this is hidden in the JLS. It states that ==
is a numerical operator. If you read the text and follow some links you can find out that char
is converted to int
. It never says explicitly that this happens also if both operands are char
but it says
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
* If either operand is of type double, the other is converted to double.
* Otherwise, if either operand is of type float, the other is converted to float.
* Otherwise, if either operand is of type long, the other is converted to long.
* Otherwise, both operands are converted to type int.
I think the last one implicitly means that char
is always converted. Also in another section it says "If either operand is not an int, it is first widened to type int by numeric promotion."
.
The warning you are getting might be very strict, but it seems to be correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With