Sonar检测Math.abs(new Random().nextInt()) “Use the original value instead”
今天早上旁边同事喊我看一个Sonar检测出的问题:

当时看了好几眼没觉得这个有太大问题,于是又看了下Sonar建议:

这是说Math.abs()方法使用在数字上面可能返回最小值,觉得这个挺有意思的,于是Google一下:
在Oracle docs: Integer Operations中有这么一段话:
The integer operators do not indicate overflow or underflow in any way.
整数的操作不会告诉我们向上溢出还是向下溢出。
所以,溢出的结果已由语言指定,独立于JVM版本,Integer.MAX_VALUE + 1 == Integer.MIN_VALUE 和 Integer.MIN_VALUE - 1 == Integer. MAX_VALUE。其他整数类型也是如此。
原子整形对象 (AtomicInteger, AtomicLong, etc.) 内部使用正常的整形去操作,所有类似getAndDecrement()方法的操作都表现一样。
所以最终把方法改成new Random().nextInt(Integer.MAX_VALUE)就好了。
参考
- Oracle doc: Integer Operations
- What happens when you increment an integer beyond its max value?
- Overflow And Underflow of Data Types in Java
- sonar问题Use the original value instead should be used on numbers that could be MIN_VALUE
Sonar检测Math.abs(new Random().nextInt()) “Use the original value instead”的更多相关文章
- Random.nextint() 和Math.random()的区别
Random.nextint() 和Math.random()的区别 Java代码 Random rand = new Random(); long startTime = System.nano ...
- random.nextInt()与Math.random()基础用法
相关文章:关于Random(47)与randon.nextInt(100)的区别 1.来源 random.nextInt() 为 java.util.Random类中的方法: Random类中还提供各 ...
- Random.nextInt()替换Math.random()
在项目中使用哪个随机数 文章参考 http://liukai.iteye.com/blog/433718 今天用了find bugs后查出来了个问题 Google了下 发现 Random.nextin ...
- JavaScript中的内置对象-8--3.Math-Math对象的方法-min()- max()- ceil() - floor()- round()- abs(); Math对象的random()方法;
JavaScript内置对象-3.Math(数值) 学习目标 1.掌握Math对象的方法: min() max() ceil() floor() round() abs() Math.min() 语法 ...
- java中常用到的math方法(Math.PI、Math.random()、Math.abs(double)、Math.floor(double)、Math.ceil(double)、Math.round(double))
public class MathDemo { public static void main(String args[]){ /** * abs求绝对值 */ System.out.println( ...
- Math类和Random类(数学公式相关类)
Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 常用方法: 1.static 数值类型 abs(数值类型 a) 返回 double 值的绝对值. 2.sta ...
- Java常用类之【Math类、Random类、System类、Runtime类】
一.Math类 Math类 [绝对值]Math.abs();//返回对应类型的绝对值 [最大值和最小值]Math.max(int a, int b) ,Math.min(int a,int b);(其 ...
- 14-02 Java Math类,Random类,System类,BigDecimal类
Math类 Math的方法 package cn.itcast_01; /* * Math:用于数学运算的类. * 成员变量: * public static final double PI * pu ...
- 08 正则表达式,Math类,Random,System类,BigInteger,BigDecimal,Date,DateFormat,Calendar
正则表达式: 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. public class Demo2_Regex { public sta ...
随机推荐
- 循序渐进VUE+Element 前端应用开发(22)--- 简化main.js处理代码,抽取过滤器、全局界面函数、组件注册等处理逻辑到不同的文件中
在我们开发代码的时候,一般都喜欢进行一定程度的重构,以达到简化代码.关注点分离.提高代码可读性等等方面的考虑,本篇随笔介绍在VUE+Element 前端应用开发过程中,实现简化main.js处理代码, ...
- 【转】Setting up SDL 2 on Visual Studio 2019 Community
FROM: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/msvc2019/index.php Setting up SDL 2 on V ...
- 09 Servlet中间服务 连接前段和后端
import 导入 在当前类中使用外包中的类时使用 cookies 缓存 Alt + enter 提示快捷键 Servlet 服务 (连接前段和后端) Servlet本质就是Java类 Ja ...
- pytorch 图像分类数据集(Fashion-MNIST)
import torch import torchvision import torchvision.transforms as transforms import matplotlib.pyplot ...
- 需要加强NB-IoT网络优化和终端监管
NB-IoT解决方案在行业规模商用应用中得到了持续的验证,承受住考验,现在芯片和网络问题和障碍已经得到解决. 从统计结果来看,目前的绝大多数问题都集中在终端侧,因为行业的定制化需求以及合伙伙伴的能力差 ...
- LoRa技术的发展应用和LoRa应用设备
LoRa技术的发展应用 LORA技术大约在十年前由法国和瑞士开发,到现今LORA技术已经是物联网发展应用中不可缺少的一部分,根据中国物联网研究与发展中心的数据,2025年我国物联网产业规模将达到2万亿 ...
- Redis常用命令(6)——SortedSet
ZADD 格式:ZADD key score member [[score member] [score member] ...] 作用:向有序集合key中插入一个或多个元素.如果元素已经存在,更新s ...
- Centos8防火墙设置
1.centos中firewalld与iptables centos7以前的版本默认使用iptables服务进行管理防火墙规则.centos7以及其以上版本默认使用firewalld服务管理防火墙.所 ...
- HashMap的put kv,是如何扩容的?
HashMap的put kv,是如何扩容的? 描述下HashMap put(k,v)的流程? 它的扩容流程是怎么样的? HashMap put(k,v)流程 通过hash(key方法)获取到key的h ...
- Ideas and Tricks Part II
33.对于统计答案幂次的技巧 对于$x^k$,考虑其组合意义:将$k$个不同球放到$x$个不同的盒子里的方案数,直接维护不好维护,那么考虑枚举把这些球放到了哪些盒子里,最后乘上第二类斯特林数和对于的阶 ...