java8 Math新增方法介紹
通常都認為java8新功能主要包括函數式編程及l(fā)ambda表達式。然而,除了那些大的特點之外,還有其他的,影響力小卻很有趣,大多時候不為人所知,甚至不太被人評論。
本文我們列舉java.lang.Math類中新增的方法,并給一些小的示例來說明。
*exact() 方法
首先看一組擴展已經存在的常用算術操作方法,從名稱及可以知其意,處理實現原有功能外,還增加了當結果溢出時拋出異常。這些方法可以使用integer和long類型作為參數。
addExact()
返回兩個參數之和,結果溢出時拋出ArithmeticException 異常:
Math.addExact(100, 50); // returns 150
Math.addExact(Integer.MAX_VALUE, 1); // throws ArithmeticException
substractExact()方法
返回兩個參數之差,結果溢出時拋出ArithmeticException 異常:
Math.subtractExact(100, 50); // returns 50
Math.subtractExact(Long.MIN_VALUE, 1); // throws ArithmeticException
incrementExact()方法
返回參數值加一,結果溢出時拋出ArithmeticException 異常:
Math.incrementExact(100); // returns 101
Math.incrementExact(Integer.MAX_VALUE); // throws ArithmeticException
decrementExact()方法
返回參數值減一,結果溢出時拋出ArithmeticException 異常:
Math.decrementExact(100); // returns 99
Math.decrementExact(Long.MIN_VALUE); // throws ArithmeticException
multiplyExact()方法
返回兩個參數之積,結果溢出時拋出ArithmeticException 異常:
Math.multiplyExact(100, 5); // returns 500
Math.multiplyExact(Long.MAX_VALUE, 2); // throws ArithmeticException
negateExact()方法
改變參數符號,結果溢出時拋出ArithmeticException 異常。我們來看看值在內存中的表示,并理解為什么會溢出,因為并不像其他exact方法那么直觀看出來:
Math.negateExact(100); // returns -100
Math.negateExact(Integer.MIN_VALUE); // throws ArithmeticException
第二個示例需要解釋下,因為不能一眼看出來:溢出是因為Integer.MIN_VALUE 是 −2.147.483.648,而Integer.MAX_VALUE 是 2.147.483.647,所以返回值超出整數范圍。
其他方法
floorDiv()
第一個參數除以第二參數,然后針對結果執(zhí)行floor操作,返回小于或等于商的整數:
Math.floorDiv(7, 2)); // returns 3
商為 3.5 ,所以 floor(3.5) == 3.
讓我們看另一個示例:
Math.floorDiv(-7, 2)); // returns -4
商為-3.5 ,所以 floor(-3.5) == -4.
modDiv()方法
該方法與前面floorDiv()方法類似, 但在模數或余數上應用floor() 操作,而不是商:
Math.modDiv(5, 3)); // returns 2
我們看到 , modDiv() 方法兩個參數為正數,和 % 操作符效果一樣。讓看看另一個不同示例:
Math.modDiv(-5, 3)); // returns 1
結果為 1 而不是 2 ,因為floorDiv(-5, 3) 是 -2 ,而不是 -1.
nextDown()方法
返回參數直接較低的值(支持 float 或 double 參數):
float f = Math.nextDown(3); // returns 2.9999998
double d = Math.nextDown(3); // returns 2.999999761581421
總結
我們描述了java8中java.lang.Math類中所有新的方法,并通過示例給與解釋說明。
補充知識:math類常用方法詳細總結
說明:Math 類位于 java.lang 包中,主要提供了一些常用的數學函數和計算
備注:一些在工程計算中才用的方法此處不做介紹
一、取整運算
1、Math.ceil(double x)
釋義:向上取整,返回大于該值的最近 double 值
System.out.println(Math.ceil(1.23)); // 2.0
System.out.println(Math.ceil(-1.23)); // -1.0
2、Math.floor(double x)
釋義:向下取整,返回小于該值的最近 double 值
System.out.println(Math.floor(1.23)); // 1.0
System.out.println(Math.floor(-1.23)); // -2.0
3、Math.round(double x)
釋義:四舍五入取整,參數類型:double、float
System.out.println(Math.round(1.43)); // 1 System.out.println(Math.round(1.53)); // 2 System.out.println(Math.round(-1.43)); // -1 System.out.println(Math.round(-1.53)); // -2
二、隨機運算
Math.random()
釋義:內部調用了 Random.nextDouble() 方法,生成一個偽均勻分布在 [0,1)之間的 double 值
System.out.println((int)(Math.random() * 10 + 1)); // 生成范圍再[1, 11)之間的偽隨機數
三、符號運算
1、Math.abs(int x)
釋義:計算絕對值,參數類型:int、long、float、double
System.out.println(Math.abs(-1)); // 1
2、Math.negateExact(int x)
釋義:計算相反值,參數類型:int、long
System.out.println(Math.negateExact(-1)); // 1
四、大小運算
1、Math.max(int x, int y)
釋義:獲取兩個參數中的最大值,參數類型:int、long、float、double
System.out.println(Math.max(1, 2)); // 2
2、Math.min(int x, int y)
釋義:獲取兩個參數中的最小值,參數類型:int、long、float、double
System.out.println(Math.min(1, 2)); // 1
3、Math.rint(double x)
釋義:獲取與參數最接近的double值
System.out.println(Math.rint(1.4)); // 1.0 System.out.println(Math.rint(1.5)); // 2.0 System.out.println(Math.rint(-1.4)); // -1.0 System.out.println(Math.rint(-1.5)); // -2.0
以上這篇java8 Math新增方法介紹就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java中valueOf和parseInt的區(qū)別詳解
這篇文章主要介紹了Java中valueOf和parseInt的區(qū)別詳解,在編程中,遇到類型轉換,好像會經常用到 parseInt 和 valueOf,當然這里只拿 Integer 類型進行陳述,其他類型也是雷同的,需要的朋友可以參考下2024-01-01
Netty之使用DelimiterBasedFrameDecoder進行消息分隔詳解
這篇文章主要介紹了Netty之使用DelimiterBasedFrameDecoder進行消息分隔詳解,在使用Netty進行TCP消息傳輸時,為了上層協(xié)議能夠對消息正確區(qū)分,避免粘包和拆包導致的問題,一般可以通過消息定長、將回車換行符作為消息結束符,需要的朋友可以參考下2023-12-12

