天天看點

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

個人名片:

🐼作者簡介:一名大一在校生 🐻‍❄️個人首頁:​​小新愛學習.​​

🕊️系列專欄:零基礎學java ----- 重識c語言

🐓每日一句:宇宙山河浪漫,生活點滴溫暖,都值得我前進。

文章目錄

  • ​​常用API​​
  • ​​Math​​
  • ​​1.1 Math概述​​
  • ​​1.2Math的常用方法​​
  • ​​2.1 System類​​
  • ​​2.2 System常用方法​​
  • ​​3.1 Object類的概述​​
  • ​​Arrays​​
  • ​​4.1 冒泡排序​​
  • ​​4.2 Arrays類的概述和常用方法​​
  • ​​5.基本類型包裝類​​
  • ​​5.1基本資料類型包裝類概述​​
  • ​​5.2 Integer 類的概述和使用​​
  • ​​5.3 int和 String 的互相轉化​​
  • ​​5.4自動裝箱和拆箱​​
  • ​​6.日期類​​
  • ​​6.1Date類概述和構造方法​​
  • ​​6.2 Date 類的常用方法​​
  • ​​6.3 SimplDateFormat 類概述​​
  • ​​Calendar類概述​​
  • ​​6.7 Calendar的常用方法​​

常用API

Math

1.1 Math概述

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

public final class Math

extends Object

Math 類包含用于執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。

沒有構造方法,

使用: 看類的成員是否都是靜态,即static修飾,如果是,直接類名調方法

1.2Math的常用方法

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
public class MathDemo {
    public static void main(String[] args) {
        //    public static int abs(int a) 傳回參數的絕對值
        System.out.println(Math.abs(-52));
        System.out.println(Math.abs(22));
//    public static double ceil(double a) 傳回大于或等于參數的最小double值,等于一個整數
        System.out.println(Math.ceil(12.24));
        System.out.println(Math.ceil(-12.33));
//    public static double floor(double a 傳回小于或等于參數的最大double值,等于一個整數
        System.out.println(Math.floor(11.25));
        System.out.println(Math.floor(11.99));
//            public static int round(float a) 按照四舍五入傳回最接近參數的int
        System.out.println(Math.round(30.65F));
        System.out.println(Math.round(29.49F));
//    public static int max(int a,int b) 傳回兩個int值中的較大值
        System.out.println(Math.max(111,999));
//    public static int min(int a,int b) 傳回兩個int值中的較小值
        System.out.println(Math.min(111,999));
//    public static double pow(double a,double b)傳回a的b次幂的值
        System.out.println(Math.pow(2,2));
//    public static double random() 傳回值為double的正值,[0.0,1.0)
        System.out.println(Math.random());

    }

}      

2.1 System類

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

public final class

Systemextends Object

System 類包含一些有用的類字段和方法。它不能被執行個體化。被static稀釋,直接類名調對象

在 System 類提供的設施中,有标準輸入、标準輸出和錯誤輸出流;對外部定義的屬性和環境變量的通路;加載檔案和庫的方法;還有快速複制數組的一部分的實用方法。

2.2 System常用方法

/*
system 常用方法
 */
public class SystemDemo {
    public static void main(String[] args) {
        //System.out.println("開始");
        //public static void exit(int status),終止目前運作的java虛拟機,非零表示異常終止
        //System.exit(0);

        //System.out.println("結束");
        //public static void currenTimeMillis(),傳回目前時間,以毫秒為機關
        System.out.println(System.currentTimeMillis() / 1000 / 60 / 60 / 24 / 365);

        //計算
        long start = System.currentTimeMillis();
        for (int i = 0; i<1000000; i++){
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println((double)(end - start)/1000);

    }
}      
詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

3.1 Object類的概述

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

public class Object類

Object 是類層次結構的根類。每個類都使用 Object 作為超類。所有對象(包括數組)都實作這個類的方法。

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

equals方法

public boolean equals(Object obj)訓示其他某個對象是否與此對象“相等”。

equals 方法在非空對象引用上實作相等關系:

自反性:對于任何非空引用值 x,x.equals(x) 都應傳回 true。

對稱性:對于任何非空引用值 x 和 y,當且僅當 y.equals(x) 傳回 true 時,x.equals(y) 才應傳回 true。

傳遞性:對于任何非空引用值 x、y 和 z,如果 x.equals(y) 傳回 true,并且 y.equals(z) 傳回 true,那麼 x.equals(z) 應傳回 true。

一緻性:對于任何非空引用值 x 和 y,多次調用 x.equals(y) 始終傳回 true 或始終傳回 false,前提是對象上 equals 比較中所用的資訊沒有被修改。

對于任何非空引用值 x,x.equals(null) 都應傳回 false。

Object 類的 equals 方法實作對象上差别可能性最大的相等關系;即,對于任何非空引用值 x 和 y,當且僅當 x 和 y 引用同一個對象時,此方法才傳回 true(x == y 具有值 true)。

學生類:

public class Student {
    private String name;
    private int age;

    public Student(){

    }
    public Student(String name, int age){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }


}      

測試類:

public class ObjectDemo {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("hmm");
        s1.setAge(19);

        Student s2 = new Student();
        s2.setName("hmm");
        s2.setAge(19);

        System.out.println(s1.equals(s2));//未重寫equals方法,比較的是位址

    }
}      

注意:當此方法被重寫時,通常有必要重寫 hashCode 方法,以維護 hashCode 方法的正常協定,該協定聲明相等對象必須具有相等的哈希碼。

參數:

obj - 要與之比較的引用對象。

傳回:

如果此對象與 obj 參數相同,則傳回 true;否則傳回 false。

另請參見:

hashCode(), Hashtable

toString方法

public String toString()傳回該對象的字元串表示。通常,toString 方法會傳回一個“以文本方式表示”此對象的字元串。結果應是一個簡明但易于讀懂的資訊表達式。建議所有子類都重寫此方法。

Object 類的 toString 方法傳回一個字元串,該字元串由類名(對象是該類的一個執行個體)、at 标記符“@”和此對象哈希碼的無符号十六進制表示組成。換句話說,該方法傳回一個字元串,它的值等于:

getClass().getName() + ‘@’ + Integer.toHexString(hashCode())

傳回:

該對象的字元串表示形式。

學生類:

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}      

測試類:

public class ObjectDemo {
    public static void main(String[] args) {
        Student s  =new Student();
        s.setName("hmm");
        s.setAge(10);
        System.out.println(s);
        System.out.println(s.toString());
    }
}      
詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

Arrays

4.1 冒泡排序

排序:将一組資料按照固定規則進行排列

冒泡排序: 一種排序的方法,對于進行的資料排序中相鄰的資料進行兩兩比較,将較大的資料放在後面,依次對所有資料進行操作,直至所有資料按要求完成排序

1. 算法步驟

比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。

對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最後一對。這步做完後,最後的元素會是最大的數。

針對所有的元素重複以上的步驟,除了最後一個。

持續每次對越來越少的元素重複上面的步驟,直到沒有任何一對數字需要比較。

圖示:

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
public class BubbleSort implements IArraySort {

    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        // 對 arr 進行拷貝,不改變參數内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        for (int i = 1; i < arr.length; i++) {
            // 設定一個标記,若為true,則表示此次循環沒有進行交換,也就是待排序列已經有序,排序已經完成。
            boolean flag = true;

            for (int j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;

                    flag = false;
                }
            }

            if (flag) {
                break;
            }
        }
        return arr;
    }
}      

注:

  • 如果有n個資料進行排序,總共需要比較n-1次
  • 每一次比較完畢,下一次比較就會少一個資料參與

4.2 Arrays類的概述和常用方法

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

public class Arrays

extends Object

此類包含用來操作數組(比如排序和搜尋)的各種方法。此類還包含一個允許将數組作為清單來檢視的靜态工廠。

除非特别注明,否則如果指定數組引用為 null,則此類中的方法都會抛出 NullPointerException。

此類中所含方法的文檔都包括對實作 的簡短描述。應該将這些描述視為實作注意事項,而不應将它們視為規範 的一部分。實作者應該可以随意替代其他算法,隻要遵循規範本身即可。(例如,sort(Object[]) 使用的算法不必是一個合并排序算法,但它必須是穩定的。)

此類是 Java Collections Framework 的成員。

toString 方法

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

sort方法

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
import java.util.Arrays;

public class ArraysDemo {
    public static void main(String[] args) {
        int arr[] = {10,52,555,74,2,6,5};
        System.out.println("排序前"+Arrays.toString(arr));
        Arrays.sort(arr);

        System.out.println("排序後"+Arrays.toString(arr));
    }
}      
詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

注:工具類的設計思想

  • 構造方法用private修飾
  • 成員用public static 修飾

5.基本類型包裝類

5.1基本資料類型包裝類概述

将基本資料類型封裝成對象的好處在于可以在對象中定義更多的功能方法操作該資料

常用的操作之一:用于基本資料類型與字元串之間的轉換

基本資料類型和包裝類的差別

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

差別:

  1. 包裝類是對象,擁有方法和字段,對象的調用都是通過引用對象的位址,基本資料類型不是
  2. 包裝類型是引用的傳遞,基本類型是值的傳遞
  3. 聲明方式不同、基本資料類型不需要new關鍵字,而包裝類型需要new在堆記憶體中進行new來配置設定記憶體空間
  4. 存儲位置不同,基本資料類型直接将值儲存在值棧中,而包裝類型是把對象放在堆中,然後通過對象的引用來調用它們
  5. 初始值不同,eg:int的初始值為0、boolean的初始值為fales ,而包裝類型的初始值為null
  6. 使用方法不同,基本資料類型直接指派使用就好,而包裝類型是在集合如collction Map時會使用

5.2 Integer 類的概述和使用

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

構造方法

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

Integer :類在對象中包裝了一個基本類型 int 的值。Integer 類型的對象包含一個 int 類型的字段。

此外,該類提供了多個方法,能在 int 類型和 String 類型之間互相轉換,還提供了處理 int 類型時非常有用的其他一些常量和方法。

public class IntegerDemo {
    public static void main(String[] args) {
        //構造方法摘要
//Integer(int value)
//          構造一個新配置設定的 Integer 對象,它表示指定的 int 值。
//Integer(String s)
//          構造一個新配置設定的 Integer 對象,它表示 String 參數所訓示的 int 值。
//
        Integer i = new Integer(100);
        System.out.println(i);

        Integer i1 = new Integer("1000");//由數字組成的字元串
        System.out.println(i1);

        //static Integer valueOf(int i)
        //          傳回一個表示指定的 int 值的 Integer 執行個體。
        //static Integer valueOf(String s)
        //          傳回儲存指定的 String 的值的 Integer 對象。
        Integer i2 = Integer.valueOf(10);
        System.out.println(i2);

        Integer i3 = Integer.valueOf("10000");
        System.out.println(i3);

    }
}      

5.3 int和 String 的互相轉化

基本類型包裝類的最常見的操作就是: 用于基本類型和字元串之間的互相轉換

public class ZhuanHuan {
    public static void main(String[] args) {
        //int--->String
        // 方法一
        int number = 100;
        String i = "" + number;
        System.out.println(i);
        //方法二
        //static String valueOf(int i)
        //          傳回 int 參數的字元串表示形式。
        String i1 = String.valueOf(number);
        System.out.println(i1);

        System.out.println("-----------------");

        //String --> Integer -- > int
        String s1 = "100";
        //String --->Integer
        Integer i2 = Integer.valueOf(s1);
        // int intValue()
        //          以 int 類型傳回該 Integer 的值。
        int x = i2.intValue();
        System.out.println(x);
        //f方法二
        //static int parseInt(String s) 
        //          将字元串參數作為有符号的十進制整數進行解析。 
        int y = Integer.parseInt(s1);
        System.out.println(y);

    }
}      

5.4自動裝箱和拆箱

  • 裝箱:把基本資料類型轉換為對應包裝類類型
  • 拆箱:把包裝類型轉換為對應的基本資料類型
// - 裝箱:把基本資料類型轉換為對應包裝類類型
// - 拆箱:把包裝類型轉換為對應的基本資料類型
public class BaoChai {
    public static void main(String[] args) {
        // - 裝箱:把基本資料類型轉換為對應包裝類類型
        Integer i = Integer.valueOf(100);
        Integer ii =100;//自動裝箱Integer.valueOf(100);
        //- 拆箱:把包裝類型轉換為對應的基本資料類型
       /* ii = ii.intValue()+200;
        System.out.println(ii);*/
        ii+=200;//内部隐含ii.intValue()
        System.out.println(ii);


        Integer iii = null;
        if(iii!=null){
            iii+=300;//NullPointerException//内部隐含ii.intValue(),是以null掉對象,會觸發空指針異常
        }
    }
}      

注意: 在使用包裝類類型的時候,若果做操作,最好先判斷是否為null

我們推薦的是:隻要是對象,在使用之前就必須進行不為null判斷

6.日期類

6.1Date類概述和構造方法

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
import java.util.Date;

//Date()
//          配置設定 Date 對象并初始化此對象,以表示配置設定它的時間(精确到毫秒)。
//Date(long date)
//          配置設定 Date 對象并初始化此對象,以表示自從标準基準時間(稱為“曆元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以來的指定毫秒數。
public class DateDemo01 {
    public static void main(String[] args) {
        //public Date()
        Date date = new Date();
        System.out.println(date);

        Date date1 = new Date(1000*60*60*24);//中國在東八區+8hour
        System.out.println(date1);
    }
}      

6.2 Date 類的常用方法

方法名 說明
public long getTime() 傳回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數。
public void setTime(long time) 設定此 Date 對象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以後 time 毫秒的時間點。
import java.util.Date;

/*
public long getTime()傳回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數。
傳回:
自 1970 年 1 月 1 日 00:00:00 GMT 以來此日期表示的毫秒數。
setTime
public void setTime(long time)設定此 Date 對象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以後 time 毫秒的時間點。
參數:
time - 毫秒數。

 */
public class DateDemo02 {
    public static void main(String[] args) {
        Date date = new Date();
        long time = date.getTime();
        System.out.println(time / 365 / 24 / 60 / 60 / 1000);
        //public void setTime(long time)設定此 Date 對象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以後 time 毫秒的時間點。
        Date date1 = new Date();
        long time1  = 1000*60*60;
        date1.setTime(time1);
        System.out.println(date1);
    }
}      

6.3 SimplDateFormat 類概述

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

public class SimpleDateFormatextends DateFormatSimpleDateFormat 是一個以與語言環境有關的方式來格式化和解析日期的具體類。它允許進行格式化(日期 -> 文本)、解析(文本 -> 日期)和規範化。

SimpleDateFormat 使得可以選擇任何使用者定義的日期-時間格式的模式。但是,仍然建議通過 DateFormat 中的 getTimeInstance、getDateInstance 或 getDateTimeInstance 來建立日期-時間格式器。每一個這樣的類方法都能夠傳回一個以預設格式模式初始化的日期/時間格式器。可以根據需要使用 applyPattern 方法來修改格式模式。有關使用這些方法的更多資訊,請參閱 DateFormat。

日期和時間模式

日期和時間格式由日期和時間模式 字元串指定。在日期和時間模式字元串中,未加引号的字母 ‘A’ 到 ‘Z’ 和 ‘a’ 到 ‘z’ 被解釋為模式字母,用來表示日期或時間字元串元素。文本可以使用單引号 (’) 引起來,以免進行解釋。"’’" 表示單引号。所有其他字元均不解釋;隻是在格式化時将它們簡單複制到輸出字元串,或者在解析時與輸入字元串進行比對。

定義了以下模式字母(所有其他字元 ‘A’ 到 ‘Z’ 和 ‘a’ 到 ‘z’ 都被保留):

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

SimplDateFormat 構造方法

構造方法摘要

SimpleDateFormat()

用預設的模式和預設語言環境的日期格式符号構造 SimpleDateFormat。

SimpleDateFormat(String pattern)

用給定的模式和預設語言環境的日期格式符号構造 SimpleDateFormat。

SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)

用給定的模式和日期符号構造 SimpleDateFormat。

SimpleDateFormat(String pattern, Locale locale)

用給定的模式和給定語言環境的預設日期格式符号構造 SimpleDateFormat。

Calendar類概述

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)

Calendar 類是一個抽象類,它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 月曆字段之間的轉換提供了一些方法,并為操作月曆字段(例如獲得下星期的日期)提供了一些方法。瞬間可用毫秒值來表示,它是距曆元(即格林威治标準時間 1970 年 1 月 1 日的 00:00:00.000,格裡高利曆)的偏移量。

該類還為實作包範圍外的具體月曆系統提供了其他字段和方法。這些字段和方法被定義為 protected。

與其他語言環境敏感類一樣,Calendar 提供了一個類方法 getInstance,以獲得此類型的一個通用的對象。Calendar 的 getInstance 方法傳回一個 Calendar 對象,其月曆字段已由目前日期和時間初始化

Calendar rightNow = Calendar.getInstance();      

Calendar 對象能夠生成為特定語言和月曆風格實作日期-時間格式化所需的所有月曆字段值**

詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)
import java.util.Calendar;

public class CalendarDemo {
    public static void main(String[] args) {
        //擷取對象
     Calendar c =   Calendar.getInstance();//多态建立對象
//        System.out.println(c);//[time=1655449283220,
        //public int get (int filed)
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        int date = c.get(Calendar.DATE);
        System.out.println(year+"年"+month+"月"+date+"日");
    }
}      

6.7 Calendar的常用方法

int get(int field)

傳回給定月曆字段的值。

abstract void add(int field, int amount)

根據月曆的規則,為給定的月曆字段添加或減去指定的時間量。

void set(int year, int month, int date)

設定月曆字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。

import java.util.Calendar;

public class CalendarDemo {
    public static void main(String[] args) {
        //擷取對象
     Calendar c =   Calendar.getInstance();//多态建立對象
//        System.out.println(c);//[time=1655449283220,
        //public int get (int filed)


        //輸出十五年後的,三個月後的,十天前
        c.add(Calendar.YEAR,15);
        c.add(Calendar.MONTH,3);
        c.add(Calendar.DATE,-10);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        int date = c.get(Calendar.DATE);
        System.out.println(year+"年"+month+"月"+date+"日");

    }
}      
詳講Java開發中的六個常用API(Math,System,Object,Integer,Date)