La spiegazione di ciò è nascosta nel JLS. Si afferma che ==
è un numerical operator. Se leggi il testo e segui alcuni link, puoi scoprire che char
viene convertito in int
. E non dice mai esplicitamente che anche questo accade se entrambi gli operandi sono char
ma 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.
penso che l'ultimo significa implicitamente che char
viene sempre convertito. Anche in another section dice "If either operand is not an int, it is first widened to type int by numeric promotion."
.
L'avviso che si sta ricevendo potrebbe essere molto severo, ma sembra essere corretto.
fonte
2013-10-22 14:40:14
Non si verifica in Eclipse, forse un'implementazione errata in IntelliJ? O i livelli di avviso sono impostati molto in alto nel tuo IDE? –
Con Idea 13 non produce alcun avviso. – Admit
È necessario attivare l'ispezione "Numeri numerici/Conversione numerica implicita". Tuttavia, non sono interessato a sbarazzarmi dell'avvertimento. Voglio capire di cosa si tratta e migliorare il mio codice – yegor256