Java常用类之【Math类、Random类、System类、Runtime类】
一、Math類
Math類
【絕對值】Math.abs();//返回對應類型的絕對值
【最大值和最小值】Math.max(int a, int b) ,Math.min(int a,int b);(其他類型都可以)
【立方根】Math.cbrt(double a) ,返回double類型
【求冪】Math.pow(double a, double b) (參數(shù)1:底數(shù),參數(shù)2:指數(shù))
【正平方根】Math.sqrt(double a)
【隨機數(shù)[0,1)】random() 返回帶正號的 double 值,該值大于等于 0.0 且小于 1.0
【取整】
ceil(double a) ,返回大于給定數(shù)的最小整數(shù)
floor(double a) ,返回小于給定數(shù)的最大整數(shù)
【四舍五入】
static int round(float a)
static long round(double a)
二、Random類(隨機數(shù)類)
random類
【作用】用于生成隨機數(shù)
Random類方法
調(diào)用方式:Random對象.方法
三、System類
System類方法:
【清理內(nèi)存】System.gc();// 促進垃圾回收器 起來回收垃圾
【退出程序】System.exit(0);// 正常退出程序 無論運行到哪個位置 都是直接退出程序
【系統(tǒng)當前時間】long l = System.currentTimeMillis();//返回以毫秒為單位的當前時間(從1970年到現(xiàn)在的毫秒數(shù))
四、Runtime類
Runtime類特點:
Runtime 類實例使應用程序能夠與其運行的環(huán)境相連接
Runtime是單例模式的
Runtime類常用方法:
【當前可用內(nèi)存】long freeMemory() ;返回 Java 虛擬機中的空閑內(nèi)存量
【創(chuàng)建對象】Runtime runtime = Runtime.getRuntime();//創(chuàng)建對象
【清理內(nèi)存】runtime.gc();// 建議回收垃圾
查看gc()方法清理內(nèi)存的效果:
System.out.println(runtime.freeMemory());//回收前的空閑內(nèi)存
runtime.gc();// 建議回收垃圾
System.out.println(runtime.freeMemory());//回收后的空閑內(nèi)存
總結(jié)
以上是生活随笔為你收集整理的Java常用类之【Math类、Random类、System类、Runtime类】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java常用类之【字符串相关类型】
- 下一篇: Java常用类之【日期相关类】