我有这个代码,它在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)
Run Code Online (Sandbox Code Playgroud)
对于代码:
String receivedMessage = "";
char c;
boolean isValidChar;
do {
c = (char) in.read();
isValidChar = (c != '\r') && (c != -1);
if (isValidChar) {
receivedMessage += c;
}
} while (isValidChar);
Run Code Online (Sandbox Code Playgroud)
char的值是16位非负的:
char:char数据类型是一个16位Unicode字符.它的最小值为'\ u0000'(或0),最大值为'\ uffff'(或65,535(含)).
您正在将它与-1进行比较.
| 归档时间: |
|
| 查看次数: |
778 次 |
| 最近记录: |