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 ...
随机推荐
- build设计模式
又叫生成器模式 public class MainActivity extends AppCompatActivity { TextView textView; Button button; prot ...
- 必须掌握的分布式文件存储系统—HDFS
HDFS(Hadoop Distributed File System)分布式文件存储系统,主要为各类分布式计算框架如Spark.MapReduce等提供海量数据存储服务,同时HBase.Hive底层 ...
- 当eclipse调用tomcat的时候发生了什么?
下午在看<tomcat权威指南>的时候想到了这么一个问题:当我在eclipse里调用本机上的一个tomcat运行一个web项目的时候,都发生了什么? 之前做本科毕设的时候用的是MyEcli ...
- D. Regular Bridge 解析(思維、圖論)
Codeforce 550 D. Regular Bridge 解析(思維.圖論) 今天我們來看看CF550D 題目連結 題目 給你一個\(k\le100\),請構造出一個至少有一個Bridge的,每 ...
- SpringBoot+Mybatis_Plus Generator
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity.Mapper.Mapper XML.Service.Control ...
- 基于ArcGIS ModelBuilder的GDB批量分区裁剪——可保留原始GDB要素集要素类结构
文章版权由作者pxtgis和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/pxtgis/. 一.概述 在数据处理工作中经常遇到批量裁剪ArcGIS文件地理数据库( ...
- 阿里P6晋升到P7是一个坎吗? P7 晋升总结
作者:程序之心丁仪 来源:https://chengxuzhixin.com/blog/post/P6_jin_sheng_dao_P7_zong_jie.html 公众号停更了挺长一段时间,首先说声 ...
- 利用命令行将本地代码上传至GitHub仓库
p.p1 { margin: 0 0 0 60px; font: 16px "PingFang SC"; color: rgba(88, 110, 117, 1) } p.p2 { ...
- GAMES101系列笔记一 图形学概述与线性代数入门
概述+线性代数 为什么学习图形学? Computer Graphics is AWESOME! 主要涉及内容: 光栅化 曲线和网格 光线追踪 动画与模拟 Differences between CG ...
- IDEA安装leetcode editor插件
leetcode > https://leetcode-cn.com/ 本地idea刷题可以直接同步提交,测试等相关操作 需要安装leetcode editor插件 1.idea setting ...