Java Number & Math 類

Java Number & Math 類

一般地,當需要使用數字的時候,我們通常使用內建資料型別,如:byte、int、long、double 等。

例項

int a = 5000; float b = 13.65f; byte c = 0x4a;

然而,在實際開發過程中,我們經常會遇到需要使用物件,而不是內建資料型別的情形。為了解決這個問題,Java 語言為每一個內建資料型別提供了對應的包裝類。

所有的包裝類(Integer、Long、Byte、Double、Float、Short)都是抽象類 Number 的子類。

包裝類 基本資料型別
Boolean boolean
Byte byte
Short short
Integer int
Long long
Character char
Float float
Double double

Java Number類

這種由編譯器特別支援的包裝稱為裝箱,所以當內建資料型別被當作物件使用的時候,編譯器會把內建型別裝箱為包裝類。相似的,編譯器也可以把一個物件拆箱為內建型別。Number 類屬於 java.lang 包。

下面是一個使用 Integer 物件的例項:

Test.java 檔案程式碼:

public class Test{ public static void main(String[] args){ Integer x = 5; x = x + 10; System.out.println(x); } }

以上例項編譯執行結果如下:

15

當 x 被賦為整型值時,由於x是一個物件,所以編譯器要對x進行裝箱。然後,為了使x能進行加運算,所以要對x進行拆箱。


Java Math 類

Java 的 Math 包含了用於執行基本數學運算的屬性和方法,如初等指數、對數、平方根和三角函式。

Math 的方法都被定義為 static 形式,通過 Math 類可以在主函式中直接呼叫。

Test.java 檔案程式碼:

public class Test { public static void main (String []args) { System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2)); System.out.println("0度的餘弦值:" + Math.cos(0)); System.out.println("60度的正切值:" + Math.tan(Math.PI/3)); System.out.println("1的反正切值: " + Math.atan(1)); System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2)); System.out.println(Math.PI); } }

以上例項編譯執行結果如下:

90 度的正弦值:1.0
0度的餘弦值:1.0
60度的正切值:1.7320508075688767
1的反正切值: 0.7853981633974483
π/2的角度值:90.0
3.141592653589793

Number & Math 類方法

下面的表中列出的是 Number & Math 類常用的一些方法:

序號 方法與描述
1 xxxValue()
將 Number 物件轉換為xxx資料型別的值並返回。
2 compareTo()
將number物件與引數比較。
3 equals()
判斷number物件是否與引數相等。
4 valueOf()
返回一個 Number 物件指定的內建資料型別
5 toString()
以字串形式返回值。
6 parseInt()
將字串解析為int型別。
7 abs()
返回引數的絕對值。
8 ceil()
返回大於等於( >= )給定引數的的最小整數,型別為雙精度浮點型。
9 floor()
返回小於等於(<=)給定引數的最大整數 。
10 rint()
返回與引數最接近的整數。返回型別為double。
11 round()
它表示四捨五入,演算法為 Math.floor(x+0.5),即將原來的數字加上 0.5 後再向下取整,所以,Math.round(11.5) 的結果為12,Math.round(-11.5) 的結果為-11。
12 min()
返回兩個引數中的最小值。
13 max()
返回兩個引數中的最大值。
14 exp()
返回自然數底數e的引數次方。
15 log()
返回引數的自然數底數的對數值。
16 pow()
返回第一個引數的第二個引數次方。
17 sqrt()
求引數的算術平方根。
18 sin()
求指定double型別引數的正弦值。
19 cos()
求指定double型別引數的餘弦值。
20 tan()
求指定double型別引數的正切值。
21 asin()
求指定double型別引數的反正弦值。
22 acos()
求指定double型別引數的反餘弦值。
23 atan()
求指定double型別引數的反正切值。
24 atan2()
將笛卡爾座標轉換為極座標,並返回極座標的角度值。
25 toDegrees()
將引數轉化為角度。
26 toRadians()
將角度轉換為弧度。
27 random()
返回一個隨機數。

Math 的 floor,round 和 ceil 方法例項比較

引數 Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

floor,round 和 ceil 例項:

public class Main { public static void main(String[] args) { double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 }; for (double num : nums) { test(num); } } private static void test(double num) { System.out.println("Math.floor(" + num + ")=" + Math.floor(num)); System.out.println("Math.round(" + num + ")=" + Math.round(num)); System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num)); } }

以上例項執行輸出結果為:

Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0