JAVA學習第四十五課 — 其它對象API(一)System、Runtime、Math類

來源:互聯網
上載者:User

標籤:四十   div   round   建立   port   double   obj   1.5   href   

一、System類

1.

static long currentTimeMillis() 返回以毫秒為單位的目前時間。


實際上:目前時間與國際標準時間 1970 年 1 月 1 日午夜之間的時間差(以毫秒為單位測量)。

long time = 1414069291407l;//long型  後面加lSystem.out.println(time);

2.

static void exit(int status)終止當前正在執行的 JAVA 虛擬機器。

static void gc()執行記憶體回收行程

3.重點

staticProperties getProperties 確定當前的系統屬性。
import java.util.Properties;import java.util.Set;public class Main {//擷取當前作業系統的分行符號public static final String LINE_SEPARATOR = System.getProperty("line.separator");public static void main(String[] args) {//擷取系統屬性資訊。並儲存到Properties集合中//properties集合儲存的都是String類型的索引值對。包java.util.Hashtable<Object,Object>//最好使用自己的儲存和取出方法來完畢元素操作/* Set<String> stringPropertyNames()          返回此屬性列表中的鍵集。當中該鍵及其相應值是字串。假設在主屬性列表中未找到同名的鍵。則還包含預設屬性列表中不同的鍵。

*/ Properties pro = System.getProperties(); Set<String> set = pro.stringPropertyNames(); for (String string : set) {String value = pro.getProperty(string);//取鍵得值System.out.println(string+"-:-"+value);} //給系統設定屬性資訊System.setProperty("myname", "xxxxxx");//這些資訊是全域,不論什麼程式都能夠調用}}


二、Runtime類

在查看API文檔的時,非常清晰的看到。Runtime沒有建構函式(說明無法建立對象),而方法摘要中卻有非靜態方法,當中有一個靜態getRuntime()方法。說明Runtime類是利用了單例設計模式設計的。

import java.io.IOException;public class Main {public static void main(String[] args) throws IOException, InterruptedException {Runtime r = Runtime.getRuntime();//返回一個進程//execute : 運行  xxxx.exe,調用本地一個程式,注意拋異常//r.exec("notepad.exe");//這樣寫會先在當前路徑下找,找不到然後會去pass路徑下找//指定路徑//r.exec("c:\\xxx\\LoL.exe");注意轉移字元r.exec("notepad.exe C:\\HelloWorld.java");//用XX程式解析XXX檔案,注意檔案要匹配Process p = r.exec("notepad.exe");Thread.sleep(1000);//顯示一秒,殺掉,注意拋異常p.destroy();// 殺掉子進程}}

三、Math類

public final class Math 
extends Object

注意不能被繼承

Math類涵蓋眾多數學函數。全然能夠挑戰所學。Math方法都是靜態

PI 圓的周長與直徑之比   E即自然對數的底數  這兩個不需定義直接

import java.io.IOException;import java.util.Random;public class Main {public static void main(String[] args){double d1 = Math.ceil(11.51);//返回大於參數的最小整數double d2 = Math.floor(11.51);//返回小於參數的最大整數double d3 = Math.round(11.51);//返回四捨五入System.out.println(d1);System.out.println(d2);System.out.println(d3);System.out.println(Math.max(4, 5));System.out.println((int)Math.pow(2, 3));//返回a的b次方for (int i = 0; i < 10; i++) {System.out.println((int)(Math.random()*10));//0-9隨機數//System.out.println(Math.floor(Math.random()*10));}Random ran = new Random();//Random對象//返回下一個偽隨機數,它是取自此隨機數產生器序列的、在 0.0 和 1.0 之間均勻分布的 double 值。

for(int i = 0;i<10;i++){//System.out.println((int)(ran.nextDouble()*6+1));//簡寫形式例如以下int d = ran.nextInt(6)+1;//nextInt是Random中的方法System.out.println(d);}}}


JAVA學習第四十五課 — 其它對象API(一)System、Runtime、Math類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.