findbug 发现的一些隐藏问题总结

RV_ABSOLUTE_VALUE_OF_RANDOM_INT 使用错误 - 尝试计算32位随机整数的绝对值

Math.abs(System.currentTimeMillis())

说明: Math.abs 不必定返回的数据是正数,下面是个例子:dom

System.out.println(Math.abs(Long.MIN_VALUE));

这个执行的结果是: -9223372036854775808ide

解释: Consider a byte. Its value ranges from -128 to 127. Say your byte has a value of -100, then Math.abs(-100) will give you 100. But what if the value of your byte is -128? You cannot represent 128 as a byte, since the maximum value it can represent is 127. So Math.abs() simply returns the negative parameter, unchanged.code

解决办法:get

Use 
    rand.nextInt(Integer.MAX_VALUE);
instead of 
    Math.abs(rand.nextInt())

参考地址: http://stackoverflow.com/questions/23435875/how-to-compute-absolute-value-of-signed-random-integerit

相关文章
相关标签/搜索