Java之常用API
阿新 • • 發佈:2021-03-28
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 -> 選擇toStringtoString
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物件,精確幾位,舍入模式) | 除法 |
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