JAVA之常見類(一)
阿新 • • 發佈:2018-12-15
JAVA之常見類(一)
一、Math類
1.概述
Math
類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函式。
2.常用函式
static double
E
比任何其他值都更接近 e(即自然對數的底數)的double
值。static double
PI
比任何其他值都更接近 pi(即圓的周長與直徑之比)的double
值。
static int
abs(int a)
返回int
值的絕對值。//向上取整
static double
ceil(double a)
返回最小的(最接近負無窮大)double
值,該值大於等於引數,並等於某個整數。//向下取整
static double
floor(double a)
返回最大的(最接近正無窮大)double
值,該值小於等於引數,並等於某個整數。
static int
max(int a, int b)
返回兩個int
值中較大的一個。
static int
min(int a, int b)
返回兩個int
值中較小的一個。
static double
pow(double a, double b)
返回第一個引數的第二個引數次冪的值。
static double
random()
返回帶正號的double
值,該值大於等於0.0
且小於1.0
。//四捨五入
static int
round(float a)
返回最接近引數的int
。
static double
sqrt(double a)
返回正確舍入的double
值的正平方根。
二、Random類
1.概述
此類的例項用於生成偽隨機數流。
如果用相同的種子建立兩個 Random
例項,則對每個例項進行相同的方法呼叫序列,它們將生成並返回相同的數字序列。為了保證此屬性的實現,為類 Random
指定了特定的演算法。
2.構造方法
Random()
建立一個新的隨機數生成器。Random(long seed)
使用單個long
種子建立一個新的隨機數生成器。
3.常用方法
int
nextInt()
返回下一個偽隨機數,它是此隨機數生成器的序列中均勻分佈的int
值。int
nextInt(int n)
返回一個偽隨機數,它是取自此隨機數生成器序列的、在 0(包括)和指定值(不包括)之間均勻分佈的int
值。
三、System類
1.概述
System
類包含一些有用的類欄位和方法。它不能被例項化。
在 System
類提供的設施中,有標準輸入、標準輸出和錯誤輸出流;對外部定義的屬性和環境變數的訪問;載入檔案和庫的方法;還有快速複製陣列的一部分的實用方法。
2.常用方法
static void
gc()
執行垃圾回收器。
static long
currentTimeMillis()
返回以毫秒為單位的當前時間。
static void
exit(int status)
終止當前正在執行的 Java 虛擬機器。
static void
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
從指定源陣列中複製一個數組,複製從指定的位置開始,到目標陣列的指定位置結束。
3.案例
案例1:
protected void
finalize()
當垃圾回收器確定不存在對該物件的更多引用時,由物件的垃圾回收器呼叫此方法。
class Demo {
@Override
public void finalize() {
System.out.println("垃圾被清掃了");
}
}
public static void demo1() {
for(int i = 0; i < 100; i++) {
new Demo();
System.gc(); //執行垃圾回收器,相當於呼喊保潔阿姨
}
}
案例2:
public static void demo2() {
System.exit(1); //非0狀態是異常終止,退出jvm
}
案例3:
public static void demo3() {
long start = System.currentTimeMillis(); //1秒等於1000毫秒
for(int i = 0; i < 1000; i++) {
System.out.println("*");
}
long end = System.currentTimeMillis(); //獲取當前時間的毫秒值
System.out.println(end - start);
}
四、BigInteger類
1.概述
不可變的任意精度的整數。
2.構造方法
BigInteger(String val)
將 BigInteger 的十進位制字串表示形式轉換為 BigInteger。
2.常用方法
BigInteger
add(BigInteger val)
返回其值為 (this + val) 的 BigInteger。
BigInteger
subtract(BigInteger val)
返回其值為 (this - val) 的 BigInteger。
BigInteger
multiply(BigInteger val)
返回其值為 (this * val) 的 BigInteger。
BigInteger
divide(BigInteger val)
返回其值為 (this / val) 的 BigInteger。
BigInteger[]
divideAndRemainder(BigInteger val)
返回包含 (this / val) 後跟 (this % val) 的兩個 BigInteger 的陣列。
五、BigDecimal類
1.概述
不可變的、任意精度的有符號十進位制數。
由於在運算的時候,float型別和double很容易丟失精度。 所以,為了能精確的表示、計算浮點數,Java提供了BigDecimal。
2.構造方法
BigDecimal(BigInteger val)
將 BigInteger 轉換為 BigDecimal
BigDecimal(String val)
將 BigDecimal 的字串表示形式轉換為 BigDecimal。
3.常用方法
BigDecimal
add(BigDecimal augend)
返回一個 BigDecimal,其值為 (this + augend),其標度為 max(this.scale(), augend.scale())。
BigDecimal
subtract(BigDecimal subtrahend)
返回一個 BigDecimal,其值為 (this - subtrahend),其標度為 max(this.scale(), subtrahend.scale())。
BigDecimal
multiply(BigDecimal multiplicand)
返回一個 BigDecimal,其值為 (this × multiplicand),其標度為 (this.scale() + multiplicand.scale())。
BigDecimal
divide(BigDecimal divisor)
返回一個 BigDecimal,其值為 (this / divisor),其首選標度為 (this.scale() - divisor.scale());如果無法表示準確的商值(因為它有無窮的十進位制擴充套件),則丟擲 ArithmeticException。
4.案例
BigDecimal bd1 = new BigDecimal(2.0); //這種方式在開發中不推薦,因為不夠精確
BigDecimal bd1 = new BigDecimal("2.0"); //通過構造中傳入字串的方式,開發時推薦
BigDecimal bd1 = BigDecimal.valueOf(2.0); //這種方式在開發中也是推薦的