今天早上旁边同事喊我看一个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. 通过maven创建springboot项目

    1,idea选择创建一个maven项目 2,pom.xml <dependencies> <dependency> <groupId>org.springframe ...

  2. 谈谈nginx和lvs各自的优缺点以及使用

            在最开始呢,咱们先说一下什么叫负载均衡,负载均衡呢,就是将一批请求,根据请求的内容,分发到不同的后端去进行相应的处理,从而提供负载分担,主备切换等功能.                 ...

  3. 直接理解转置卷积(Transposed convolution)的各种情况

    使用GAN生成图像必不可少的层就是上采样,其中最常用的就是转置卷积(Transposed Convolution).如果把卷积操作转换为矩阵乘法的形式,转置卷积实际上就是将其中的矩阵进行转置,从而产生 ...

  4. IOCP 模型2 AcceptEx

    // IOCP2.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  5. Python爬虫实战详解:爬取图片之家

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理 如何使用python去实现一个爬虫? 模拟浏览器请求并获取网站数据在原始数据 ...

  6. ner处理数据的方式

    ner处理数据的方式biodef load_data(filename): features = [] labels = [] f = open(filename, encoding='utf-8') ...

  7. 知识全聚集 .Net Core 技术突破 | 我用C#手把手教你玩微信自动化一

    知识全聚集 .Net Core 技术突破 | 我用C#手把手教你玩微信自动化一 教程 01 | 模块化方案一 02 | 模块化方案二 03 | 简单说说工作单元 其他教程预览 分库分表项目实战教程 G ...

  8. 经典c程序100例==41--50

    [程序41] 题目:学习static定义静态变量的用法 1.程序分析: 2.程序源代码: #include "stdio.h" varfunc() { int var=0; sta ...

  9. KMP算法和bfprt算法总结

    目录 1 KMP算法 1.1 KMP算法分析 1.2 KMP算法应用 题目1:旋转词 题目2:子树问题 2 bfprt算法 2.1 bfprt算法分析 2.2 bfprt算法应用 1 KMP算法 大厂 ...

  10. startup乱码解决方法

    1.用记事本方式打开:‪apache-tomcat-8.5.59\conf\logging.properties 2.使用快捷键(Ctrl+H)把UTF-8全部替换为:GBK,进行保存(Ctrl+s) ...