java常用API之System類

來源:互聯網
上載者:User

標籤:jvm   api   組元   containe   程式   cells   指定   prope   copy   

System中代表程式所在系統,提供了對應的一些系統屬性資訊,和系統操作。System類不能手動建立對象,因為構造方法被private修飾,阻止外界建立對象。System類中的都是static方法,類名訪問即可。

常用方法:

l  currentTimeMillis() 擷取當前系統時間與1970年01月01日00:00點之間的毫秒差值

l  exit(int status) 用來結束正在啟動並執行Java程式。參數傳入一個數字即可。通常傳入0記為正常狀態,其他為異常狀態

l  gc() 用來運行JVM中的記憶體回收行程,完成記憶體中垃圾的清除。

l  getProperty(String key) 用來擷取指定(字串名稱)中所記錄的系統屬性資訊

l  arraycopy方法,用來實現將源數組部分元素複製到目標數組的指定位置

System類的方法練習:

l  練習一:驗證for迴圈列印數字1-9999所需要使用的時間(毫秒)

public  static  void  main(String[] args) {       long  start = System.currentTimeMillis();      for  ( int  i=0; i<10000; i++) {           System. out .println(i); } long  end = System.currentTimeMillis(); System. out .println( "共耗時毫秒:"  + (end-start) ); }

l  練習二:將src數組中前3個元素,複製到dest數組的前3個位置上

複製元素前:src數組元素[1,2,3,4,5],dest數組元素[6,7,8,9,10]

複製元素後:src數組元素[1,2,3,4,5],dest數組元素[1,2,3,9,10]

 

public static void main(String[] args) {

int [] src =  new  int []{1,2,3,4,5}; int [] dest =  new  int []{6,7,8,9,10}; System.arraycopy( src, 0, dest, 0, 3); 代碼運行後:兩個數組中的元素髮生了變化 src數組元素[1,2,3,4,5] dest數組元素[1,2,3,9,10] }

 

l  練習三:迴圈產生100-999之間的的三位元並進行列印該數,當該數能被10整除時,結束啟動並執行程式

123456789 public static void main(String[] args){     Random random = new Random();    while(true){    int number = random.nextInt(900)+100; //0-899 + 100    if (nmumber % 10 == 0) {        System.exit(0);}}}

  

java常用API之System類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.