今天早上旁边同事喊我看一个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)就好了。

参考

Sonar检测Math.abs(new Random().nextInt()) “Use the original value instead”的更多相关文章

  1. Random.nextint() 和Math.random()的区别

    Random.nextint() 和Math.random()的区别 Java代码   Random rand = new Random(); long startTime = System.nano ...

  2. random.nextInt()与Math.random()基础用法

    相关文章:关于Random(47)与randon.nextInt(100)的区别 1.来源 random.nextInt() 为 java.util.Random类中的方法: Random类中还提供各 ...

  3. Random.nextInt()替换Math.random()

    在项目中使用哪个随机数 文章参考 http://liukai.iteye.com/blog/433718 今天用了find bugs后查出来了个问题 Google了下 发现 Random.nextin ...

  4. 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() 语法 ...

  5. 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( ...

  6. Math类和Random类(数学公式相关类)

    Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 常用方法: 1.static 数值类型 abs(数值类型 a)      返回 double 值的绝对值. 2.sta ...

  7. Java常用类之【Math类、Random类、System类、Runtime类】

    一.Math类 Math类 [绝对值]Math.abs();//返回对应类型的绝对值 [最大值和最小值]Math.max(int a, int b) ,Math.min(int a,int b);(其 ...

  8. 14-02 Java Math类,Random类,System类,BigDecimal类

    Math类 Math的方法 package cn.itcast_01; /* * Math:用于数学运算的类. * 成员变量: * public static final double PI * pu ...

  9. 08 正则表达式,Math类,Random,System类,BigInteger,BigDecimal,Date,DateFormat,Calendar

    正则表达式:    是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. public class Demo2_Regex { public sta ...

随机推荐

  1. Chrome FeHelper 插件下载地址

    方便大家下载 下载可用,本人亲自测试 下载地址: 地址链接

  2. 关于oracle监听程序的相关问题及解决方法

    1.查看监听程序是否启动 打开cmd窗口,cmd用管理员运行,否则无法执行启动与停止监听命令 lsnrctl status查看运行状态 lsnrctl stop停止监听 lsnrctl start启动 ...

  3. ArcPy批量选择指定属性的要素

    在GIS数据处理中,选择数据是十分频繁的操作,常用的是"按属性选择"和"按位置选择",这两个功能虽然比较强大,但有时也不能满足实际需求.比如可能时常会遇到这样一 ...

  4. Java学习的第四十六天

    1.例8.1例类 import java.util.Scanner; public class Cjava { public static void main(String[]args) { Time ...

  5. STM32移植FreeRTOS(1)

    "STM32F103VET6<_>FreeRTOS" 1.项目功能实现 1)LED灯定时闪烁 2)KEY按键检测 3)FreeRTOS任务创建 4)串口输出程序运行状态 ...

  6. C# 泛型的协变

    class Program { static void Main(string[] args) { Person person = new Person(); Person chinese2 = ne ...

  7. Jmeter——ForEach Controller&Loop Controller

    今天来分享下Jmeter中的2款循环控制器,ForEach Controller和Loop Controller,在使用上还是有所区别. ForEach Controller ForEach Cont ...

  8. 用GitHub Pages搭建博客(七)

    本篇介绍百度统计.百度搜索 一般来讲,部署了一个网站后,我们需要知道网站的浏览量,以便知道网站是否有人访问. 在Jekyll的模板中,由于国外开发者更多,一般的主题默认都开发了谷歌统计(Google ...

  9. apache+php安装

    https://codebriefly.com/how-to-setup-apache-php-mysql-on-windows-10/ 出问题的地方 1.apache rewrite 模块开启 (去 ...

  10. 利用GitHub和Hexo打造免费的个人博客

    每个程序猿都需要一个个人博客,目前广泛出现在大家视野里的有CSDN.博客园.简书,但是他们却没有给用户一个专属的站点.一个好记的域名.你需要一个https://xxx.xxx.xxx/格式的网址,一个 ...