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

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

    Math.abs(System.currentTimeMillis())

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

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

这个执行的结果是: -9223372036854775808

解释: 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.

解决办法:

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-integer

转载于:https://my.oschina.net/u/725800/blog/816227

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值