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整除時,結束運行的程序
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);
}
}
}
