標籤:越界 print 列印 封裝 記憶體 current 否則 main ati
1.System類與Runtime類1.1System類
System類對我們來說並不陌生,在之前學習的知識中,當我們需要列印結果時,使用的都是"System.out.println()"語句進行列印輸出,這句代碼中就使用了System類。這個類中定義了與系統相關的屬性和方法,它所提供的屬性和方法都是靜態,因此,想要引用這些屬性和方法,直接使用System類調用即可。下表是System類常用的一些方法。
| 方法聲明 |
功能描述 |
| static void exit(int status) |
該方法用於終止當前正在啟動並執行Java虛擬機器,其中,參數status表示狀態嗎,若狀態代碼非0,則表示異常終止 |
| static void gc() |
運行記憶體回收行程,並對垃圾進行回收 |
| static native long currentTimeMillis(); |
返回以毫秒為單位的目前時間 |
| static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) |
從src引用的指定源數組賦值到dest引用的數組,賦值從指定的位置開始,到目標數組的指定位置結束,會覆蓋目標數組中對應位置的元素 |
| static Properties getProperties() |
取得當前的系統屬性 |
| static String getProperty(String key) |
擷取指定鍵描述的系統屬性 |
getProperties()方法用於擷取當前系統的全部屬性,該方法會返回一個Properties對象,其中封裝了系統的所有屬性,這些屬性是以索引值對形式存在的。
Example01.java
public class Example01 { public static void main(String[] args) { //擷取當前系統的屬性 Properties properties = System.getProperties(); System.out.println(properties); //擷取所有系統屬性的key(屬性名稱),返回Set對象 Set<String> propertyNames = properties.stringPropertyNames(); for (String key : propertyNames) { //擷取當前鍵(key)對應的值(屬性值) String value = System.getProperty(key); System.out.println(key + "---->" + value); } }}
運行結果如下:
該方法返回一個long類型的值,該值表示目前時間與1970年1月1日0點0時0分0秒之間的時間差,單位是毫秒,通常也將該值稱為時間戳記。
Example02.java
public class Example02 { public static void main(String[] args) { long startTime = System.currentTimeMillis();//迴圈開始的目前時間 int sum = 0; for (int i = 0; i < 100000000; i++) { sum += i; } long endTime = System.currentTimeMillis();//迴圈結束後的目前時間 System.out.println("程式啟動並執行時間為:" + (endTime - startTime) + "毫秒"); }}
運行結果如下:
- arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
arraycopy()方法用於將一個數組中的元素快速拷貝到另一個數組,其中參數的具體作用如下:
src:表示源數組。
dest:表示目標數組。
srcPost:表示源數組中拷貝元素的起始位置。
destPos:表示拷貝到目標數組的起始位置。
length:表示拷貝元素的個數。
需要注意的是,在進行數組複製時,源數組中必須有足夠拷貝個數的元素存在,同時目標數組必須有足夠的空間來存放拷貝的元素,否則會發生角標越界異常。
Example03.java
public class Example03 { public static void main(String[] args) { int[] fromArray = {101,102,103,104,105,106};//源數組 int[] toArray = {201,202,203,204,205,206,207};//目標數組 System.arraycopy(fromArray, 2, toArray, 3, 4); //列印目標數組中的元素 for (int i = 0; i < toArray.length; i++) { System.out.println(i + ":" + toArray[i]); } }}
運行結果如下:
除了上面例子中的方法外,System類還有兩個常見的方法,分別是gc()和exit(int status)方法。其中,gc()方法用來啟動Java的記憶體回收行程,並且對記憶體中的垃圾對象進行回收。exit(int status)方法用來終止當前正在啟動並執行Java虛擬機器,其中的參數status用於表示當前發生的異常狀態,通常指定為0,表示正常退出,否則表示異常終止。
1.2Runtime類
Runtime類表示虛擬機器運行時的狀態,用於封裝虛擬機器進程。每次使用Java命令啟動虛擬機器都對應一個Runtime執行個體,並且只有一個執行個體,因此該類採用單例模式進行設計,對象不可以直接執行個體化。若想在程式中擷取一個Runtime執行個體,只能通過以下方法:
Runtime run = Runtime.getRuntime();
由於Runtime執行個體中封裝了虛擬機器進程,因此在程式中通常會通過該類的執行個體對象來擷取當前虛擬機器的相關資訊。
Example04.java
public class Example04 { public static void main(String[] args) { Runtime rt = Runtime.getRuntime();//擷取Runtime執行個體 System.out.println("處理器的個數:" + rt.availableProcessors() + "個"); System.out.println("空閑記憶體數量:" + rt.freeMemory()/1024/1024 + "M"); System.out.println("最大可用記憶體數量:" + rt.maxMemory()/1024/1024 + "M"); }}
運行結果如下:
在上面的例子中,availableProcessors()方法,freeMemory()方法和maxMemory()方法分別計算當前虛擬機器的處理器個數,空閑記憶體數和最大記憶體數量,單位是位元組。
Runtime類提供了一個exec()方法,該方法用於執行一個dos命令,從而實現與在命令列視窗中輸入dos命令同樣的效果,例子如下:
Example05.java
public class Example05 { public static void main(String[] args) throws IOException { Runtime rt = Runtime.getRuntime();//建立Runtime執行個體對象 rt.exec("notepad.exe");//調用exec()方法 }}
Runtime類的exec()方法會返回一個Process對象,該對象表示作業系統的一個進程,通過該對象可以對產生的新進程進行管理,如需要關閉進程 只需要調用destroy()方法即可。
Example06.java
public class Example06 { public static void main(String[] args) throws IOException, InterruptedException { Runtime rt = Runtime.getRuntime();//建立Runtime執行個體對象 Process process = rt.exec("notepad.exe");//調用exec()方法 Thread.sleep(3000);//程式休眠3秒 process.destroy();//殺掉進程 }}
JAVA API(二)System類與Runtime類