public static void sysProp()throws Exception{
Map<String,String> env = System.getenv();
//獲取系統的所有環境變量
for(String name : env.keySet()){
System.out.println(name + " : " +env.get(name));
}
//獲取系統的指定環境變量的值
System.out.println(env.get("JAVA_HOME"));
//獲取系統的所有屬性
Properties prop = System.getProperties();
//將系統的屬性保存到配置文件中去
prop.store(new FileOutputStream("Prop.properties"),"System properties");
//輸出特定的系統屬性
System.out.println(System.getProperty("os.name"));
}
2,與系統時間有關的方法操作
public static void sysTime(){
//獲取系統當前的時間毫秒currentTimeMillis()(返回當前時刻距離UTC 1970.1.1 00:00的時間差)
Long time = System.currentTimeMillis();
System.out.println(time);
Long time1 = System.nanoTime();//主要用于計算時間差單位納秒
Long time3 = System.currentTimeMillis();
for(Long i =0l ;i <999l; i++){}
Long time2 = System.nanoTime();
Long time4 = System.currentTimeMillis();
System.out.println(time2 - time1+ " : " +(time4 - time3));
}
3,鑒別兩個對象在堆內存當中是否是同一個
public static void identityHashCode(){
//str1 str2為兩個不同的String對象
String str1 = new String("helloWorld");
String str2 = new String("helloWorld");
//由于String類重寫了hashCode()方法 所以 他們的HashCode是一樣的
System.out.println(str1.hashCode()+" : "+str2.hashCode());
//由于他們不是同一個對象所以他們的計算出來的HashCode時不同的。
//實際上該方法使用的時最原始的HashCode計算方法即Object的HashCode計算方法
System.out.println(System.identityHashCode(str1) + " : "+ System.identityHashCode(str2));
String str3 = "hello";
String str4 = "hello";
//由于他們引用的是常量池中的同一個對象 所以他們的HashCode是一樣的
System.out.println(System.identityHashCode(str3) + " : "+ System.identityHashCode(str4));
/*輸出如下
-1554135584 : -1554135584
28705408 : 6182315
21648882 : 21648882
*/
}
Runtime類的常用用法
每個 Java 應用程序都有一個 Runtime 類實例,使應用程序能夠與其運行的環境相連接。
class RunTimeTest
{
public static void main(String[] args) throws Exception
{
getJvmInfo();
//execTest();
}
public static void getJvmInfo(){
//獲取Java運行時相關的運行時對象
Runtime rt = Runtime.getRuntime();
System.out.println("處理器數量:" + rt.availableProcessors()+" byte");
System.out.println("Jvm總內存數 :"+ rt.totalMemory()+" byte");
System.out.println("Jvm空閑內存數: "+ rt.freeMemory()+" byte");
System.out.println("Jvm可用最大內存數: "+ rt.maxMemory()+" byte");
}
public static void execTest()throws Exception{
Runtime rt = Runtime.getRuntime();
//在單獨的進程中執行指定的字符串命令。
rt.exec("mspaint E:\\mmm.jpg");
}
}