java种System、Runtime、indentityHashCode的使用
/** System类的使用 里面有in、out、err 输入(一般是键盘),输出(一般是显示器),和错误输出可以用setIn设置标准输入输出流。 */ package frank; import java.lang.*; import java.util.Map; import java.io.FileOutputStream; import java.util.Properties; public class App { public static void main(String[] args) throws Exception { /*--------------------系统属性和环境变量的获取-------------*/ //获得系统所有的环境变量 Map<String,String> env = System.getenv(); for(String name : env.keySet()) { System.out.println(name+"--->"+env.get(name)); } //获得指定的环境变量值 System.out.println(System.getenv("Path")); //获得所有的系统属性 Properties props = System.getProperties(); //将所有的系统属性保存到props.txt文件中 props.store(new FileOutputStream("props.txt"),"System Properties"); //输出特定的系统属性 System.out.println(System.getProperty("os.name")); System.gc(); System.runFinalization(); /*-------------------hashCode和identityHashCode的对比------------*/ String s1 = new String("hello"); String s2 = new String("hello"); //两个字符序列相等,所以他们的hashCode也相等。 System.out.println(s1.hashCode()+"----"+s2.hashCode()); //s1和s2是不同的字符串对象,所以他们的identityHashCode值不同 System.out.println(System.identityHashCode(s1)+"----"+System.identityHashCode(s2)); String s3 = "java"; String s4 = "java"; //他们两个指向字符串常量池中同一个对象,所以他们的identityHashCode值相等。 System.out.println(System.identityHashCode(s3)+"----"+System.identityHashCode(s4)); /*------------------------Runtime类的使用---------------------------------------------*/ //每个java程序都会有一个对应的Runtime,应用程序不能创建自己的Runtime,但是可以通过getRuntime方法获得自己的Runtime Runtime rt = Runtime.getRuntime(); System.out.println("处理器数量:"+rt.availableProcessors());//获得处理器数量 System.out.println("空闲内存:"+rt.freeMemory());//获得空闲内存 System.out.println("总内存:"+rt.totalMemory());//获得总内存 System.out.println("可用最大内存:"+rt.maxMemory());//获得可用最大内存 rt.exec("cmd.exe");//打开记事本 } }