我有这个代码在FindBugs中显示以下错误:
Bug: Bad comparison of nonnegative value with -1 in hydra.extensions.drivers.eg2.internal.EG2GatewaySimulator$1.run()
This code compares a value that is guaranteed to be non-negative with a negative constant or zero.
Rank: Scary (5), confidence: High
Pattern: INT_BAD_COMPARISON_WITH_NONNEGATIVE_VALUE
Type: INT, Category: CORRECTNESS (Correctness)
代码:
String receivedMessage = "";
char c;
boolean isValidChar;
do {
c = (char) in.read();
isValidChar = (c != '\r') && (c != -1);
if (isValidChar) {
receivedMessage += c;
}
} while (isValidChar);
答案 0 :(得分:6)
char值为16位非负值:
char:char数据类型是一个16位Unicode字符。它的最小值为'\ u0000'(或0),最大值为'\ uffff'(或65,535(含))。
您正在将它与-1进行比较。