Java之常用API

API概述

什麼是API
API (Application Programming Interface) :應用程序編程接口
java中的API
指的就是 JDK 中提供的各種功能的 Java類,這些類將底層的實現封裝了起來,我們不需要關心這些類是如何
實現的,只需要學習這些類如何使用即可,我們可以通過幫助文檔來學習這些API如何使用。

Math

1、Math類概述
Math 包含執行基本數字運算的方法
2、Math中方法的調用方式
Math類中無構造方法,但內部的方法都是靜態的,則可以通過 類名.進行調用
3、Math類的常用方法
 
public static int abs(int a)
 
返回參數的絕對值 
public static double ceil(double a)
返回大於或等於參數的最小double值,等於一個整數
public static double floor(double a)
返回小於或等於參數的最大double值,等於一個整數
public static int round(float a) 按照四捨五入返回最接近參數的int
public static int max(int a,int b) 返回兩個int值中的較大值
public static int min(int a,int b) 返回兩個int值中的較小值
public static double pow (double a,double b)
返回a的b次冪的值
public static double random() 返回值為double的正值,[0.0,1.0) 

System

public static void exit(int status)
終止當前運行的 Java 虛擬機,非零表示異常終止
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
 
 從指定源數組中複製一個數組,複製從指定的位置開始,到目標數組的指定位置結束。從 src 引用的源數組到 dest 引用的目標數組,數組組件的一個子序列被複制下來。被複制的組件的編號等於 length 參數。
public static long currentTimeMillis() 返回當前時間(以毫秒為單位)
int [] array={1,2,3,4};
       int[] array2=new int[4];
       System.arraycopy(array,0,array2,2,2);
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
      }

Object

Object類概述
Object 是類層次結構的根,每個類都可以將 Object 作為超類。所有類都直接或者間接的繼承自該類,
換句話說,該類所具備的方法,所有類都會有一份
 
查看方法源碼的方式
選中方法,按下Ctrl + B
 
重寫toString方法的方式
1. Alt + Insert 選擇toString
2. 在類的空白區域,右鍵 -> Generate -> 選擇toString

toString

toString方法的作用:
以良好的格式,更方便的展示對象中的屬性值
public class Student {
    private int age;
    private String name;
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }
}

 

public class test {
    public static void main(String[] args) {
        Student student=new Student(10,"張三");
        System.out.println(student);
    }
}

 

 

 

輸出:Student{age=10, name=’張三’}

equals

equals方法的作用
用於對象之間的比較,返回true和false的結果
 
舉例:s1.equals(s2); s1和s2是兩個對象
 
重寫equals方法的場景
不希望比較對象的地址值,想要結合對象屬性進行比較的時候。
 
重寫equals方法的方式
1. alt + insert 選擇equals() and hashCode(),IntelliJ Default,一路next,finish即可
2. 在類的空白區域,右鍵 -> Generate -> 選擇equals() and hashCode(),後面的同上。
public class Student {
    private int age;
    private String name;

    @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 Student(int age, String name) {
        this.age = age;
        this.name = name;
    }
}
public class test {
    public static void main(String[] args) {
        Student student=new Student(10,"張三");
        Student student2=new Student(10,"張三");
        System.out.println(student.equals(student2));
    }
}

輸出:true

Objects

常用方法 
public static String toString(對象)
返回參數中對象的字符串表示形式。
public static String toString(對象, 默認字符串) 返回對象的字符串表示形式。
public static Boolean isNull(對象) 判斷對象是否為空
public static Boolean nonNull(對象) 判斷對象是否不為空
public class Student {
    private int age;
    private String name;
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }
}
import java.util.Objects;

public class test {
    public static void main(String[] args) {
        Student student=new Student(10,"張三");
//        Student student2=new Student(10,"張三");
        String result= Objects.toString(student);
        Student student2=null;
        String result2=Objects.toString(student2,"null");
        Student student3=null;
        boolean result3=Objects.isNull(student3);
        boolean result4=Objects.nonNull(student);
        System.out.println(result);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
    }
}

輸出:

Student{age=10, name=’張三’}
null
true
true

BigDecimal 

作用
可以用來進行精確計算
構造方法 
BigDecimal(double val)
參數為double
BigDecimal(String val) 參數為String 
常用方法
public BigDecimal add(另一個BigDecimal對象)
加法
public BigDecimal subtract (另一個BigDecimal對象) 減法
public BigDecimal multiply (另一個BigDecimal對象) 乘法
public BigDecimal divide (另一個BigDecimal對象) 除法
public BigDecimal divide (另一個BigDecimal對象,精確幾位,舍入模式) 除法 
總結
1. BigDecimal是用來進行精確計算的
2. 創建BigDecimal的對象,構造方法使用參數類型為字符串的。
3. 四則運算中的除法,如果除不盡請使用divide的三個參數的方法。
 
BigDecimal divide = bd1.divide(參與運算的對象,小數點後精確到多少位,舍入模式);
參數1 ,表示參與運算的BigDecimal 對象。
參數2 ,表示小數點後面精確到多少位
參數3 ,舍入模式
BigDecimal.ROUND_UP 進一法
BigDecimal.ROUND_FLOOR 去尾法
BigDecimal.ROUND_HALF_UP 四捨五入
BigDecimal bd1=new BigDecimal("10.0");
BigDecimal bd2=new BigDecimal("3.0");
BigDecimal divide = bd1.divide(bd2, 2, BigDecimal.ROUND_UP);
System.out.println(divide);

輸出:3.34

Tags: