Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA complains about "implicit numeric conversion of char to int"

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?

like image 543
yegor256 Avatar asked Oct 22 '13 13:10

yegor256


1 Answers

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.

like image 96
André Stannek Avatar answered Sep 29 '22 15:09

André Stannek