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 的子類。


    這種由編譯器特別支持的包裝稱爲裝箱,所以當內置數據類型被當作對象使用的時候,編譯器會把內置類型裝箱爲包裝類。相似的,編譯器也可以把一個對象拆箱爲內置類型。Number 類屬於 java.lang 包。下面是一個使用 Integer 對象的實例:

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 包含了用於執行基本數學運算的屬性和方法,如初等指數、對數、平方根和三角函數。Math 的方法都被定義爲 static 形式,通過 Math 類可以在主函數中直接調用。

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 類常用的一些方法:

序號 方法與描述
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

    然後咱們來看實例:

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

    再來看兩個應用實例:

/**
* @author Dale
* java中的自動裝箱與拆箱
* 簡單一點說,裝箱就是自動將基本數據類型轉換爲包裝器類型;拆箱就是自動將包裝器類型轉換爲基本數據類型。
*/
public class Number {
    public static void main(String[] args) {
        /**
        Integer i1 = 128;  // 裝箱,相當於 Integer.valueOf(128);
        int t = i1; //相當於 i1.intValue() 拆箱
        System.out.println(t);
        */

        /**
        對於–128到127(默認是127)之間的值,被裝箱後,會被放在內存裏進行重用
        但是如果超出了這個值,系統會重新new 一個對象
        */
        Integer i1 = 200;
        Integer i2 = 200;

        /**
        注意 == 與 equals的區別
        == 它比較的是對象的地址
        equals 比較的是對象的內容
        */
        if(i1==i2) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
    
Integer a = 10;
Integer b = 10;
System.out.println(a == b);        // true
System.out.println(a.equals(b));   // true
Integer a = 1000;
Integer b = 1000;
System.out.println(a == b);        // false
System.out.println(a.equals(b));   // true
    好啦,這次就到這裏了。如果感覺不錯的話,請多多點贊支持哦。。。