java System類學習筆記

來源:互聯網
上載者:User

標籤:包含   名稱   其他   blog   str   繼承   學習   static   常見   

java 中 System 類

最常見到 System.out.println();

System類 定義為 public final class System extends Object{} 包含幾個有用的類欄位和方法,用了關鍵字 final 修飾,表示此類不能被其他類繼承。

其構造方法為 private System{} (構造方法私人化,不能被外部執行個體化)。 

System 中有三個屬性:in,out,err;

1.private final static InputStream in=null;

2.private final static PrintStream out=null;  

           -->  out 為類PrintStream 類型 println()就是PrintStream中的方法。out被 final 和static 修飾的常量,故可以用System調用out屬性,再調用println()方法。

3.private final static PrintStream err=null;

System 中的幾個方法:

System 中的方法全部用 static 修飾,可以用類名稱直接調用,例如 System.getProperties();

1,public static Properties getProperties(){}      System.getProperties().list(System.out);  -->確定當前系統屬性。

2,public static String getProperties(String key){}     System.getProperty(String key);  --> 擷取當前鍵指定的系統屬性。

3,public static long currentTimeMillis(){}       System.currentTimeMillis();  --> 返回目前時間(以毫秒為單位)。

public class S{    public static void main(String[] args){        long startTime=System.currentTimeMillis();        int a=0;        for(int i=0;i<1000000000;i++){            a+=i; } long endTime=System.currentTimeMillis(); System.out.println("執行此程式用了"+(endTime-startTime)+"毫秒。"); }}

------------>      輸出為:  執行此程式用了380毫秒。

4,public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length){}    將指定源數組中的數組從指定位置複製到目標數組的指定位置。

public class S{    public static void main(String[] args){        int[] a=new int[]{1,2,3,10,20,30,4,5,6};        int[] b=new int[3];        System.arraycopy(a,3,b,0,3);        for(int i=0;i<b.length;i++){ System.out.print(b[i]+" "); }}

 ------------->      輸出為:10  20  30        ------>  a表示數組a, 3表示 a 數組座標(10),b表示數組b,0 表示 b 數組的座標 0,3表示拷貝的長度。

java 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.