Math:
代表的是數學,是一個工具類,里面提供的都是對數據進行操作的一些靜態方法。
?
示例代碼:
package cn.ensourced1_math;public class MathTest {public static void main(String[] args) {// 目標:了解Math類提供的常用方法// abs方法System.out.println(Math.abs(-11.2));System.out.println(Math.abs(123));System.out.println(Math.abs(-3.14));// ceil方法System.out.println(Math.ceil(4.0001)); // 5.0System.out.println(Math.ceil(4.0));// floor方法System.out.println(Math.floor(6.99)); // 6.0// round方法(四舍五入)System.out.println(Math.round(4.32)); // 4// Max/MinSystem.out.println(Math.max(11.2, 20)); // 20.0System.out.println(Math.min(11.2, 20)); // 11.2// pow(a, b) 次方System.out.println(Math.pow(2, 3)); // 8.0// 取隨機數System.out.println(Math.random());}
}
不要刻意去記憶,如果忘記了,可以過來查找的。
System:
System代表程序所在的系統,也是一個工具類。
package cn.ensourced1_math;public class SystemTest {public static void main(String[] args) {// 目標;了解下System類的常見方法// System.exit(0); // 非零表示異常終止// 獲取系統時間long time = System.currentTimeMillis(); // 1970年來的毫秒值System.out.println(time);}
}
?我們正常拿這個時間來做什么呢?
做代碼的性能分析。
Runtime:
代表程序所在的運行環境。
Runtime是一個單例類。
package cn.ensourced1_math;import java.io.IOException;public class RuntimeTest {public static void main(String[] args) throws IOException, InterruptedException {// Runtime類Runtime r = Runtime.getRuntime();// 終止當前運行的虛擬機
// r.exit(0); // 非零表示異常終止// Java虛擬機可用的處理器數System.out.println(r.availableProcessors());// 內存總量System.out.println(r.totalMemory()/(1024 * 1024) + "MB");// 可用內存System.out.println(r.freeMemory()/1024.0/1024.0 + "MB");// 啟動某個程序
// r.exec("C:\\Program Files (x86)\\XMind\\XMind.exe");Process p = r.exec("C:\\Program Files\\Tencent\\QQNT\\QQ.exe"); // 直接絕對路徑Thread.sleep(5000);p.destroy();}
}