Java的System类,Runtime类
来源:互联网 发布:windows swift 编译器 编辑:程序博客网 时间:2024/05/22 03:41
System类代表当前Java程序的支行平台,程序不能创建System类的对象。
获得环境变量:
public class SystemTest{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("JAVA_HOME"));// 获取所有的系统属性Properties props = System.getProperties();// 将所有系统属性保存到props.txt文件中props.store(new FileOutputStream("props2.txt"), "System Properties");// 输出特定的系统属性System.out.println(System.getProperty("os.name"));}}
System类还提供了一个identityHashCode(Object x)方法,该方法返回指定对象的精确hashCode值,也就是根据该对象的地址计算得到的hashCode值。当某个类的hashCode()方法被重写后,该类实例的hashCode()方法就不能唯一地标识该对象;但通过identityHashCode()方法返回的hashCode值,依然是根据该对象的地址计算得到的hashCode值。所以,如果两个对象的identityHashCode值相同,则两个对象绝对是同一个对象。
public class IdentityHashCodeTest{public static void main(String[] args){// 下面程序中s1和s2是两个不同对象String s1 = new String("Hello");String s2 = new String("Hello");// String重写了hashCode()方法——改为根据字符序列计算hashCode值,// 因为s1和s2的字符序列相同,所以它们的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";// s3和s4是相同的字符串对象,所以它们的identityHashCode值相同System.out.println(System.identityHashCode(s3)+ "----" + System.identityHashCode(s4));}}
Runtime类代表Java程序的运行时环境,每个Java程序都有一个与之对应的Runtime实例,应用程序通过该对象与其运行时环境相连。应用程序不能自己创建自己的Runtime实例,但可以通过getRuntime()方法获取与之关联的Runtime对象。
通过Runtime类可以访问JVM的相关信息,如处理器数据,内存信息等。
public class RuntimeTest{public static void main(String[] args){// 获取Java程序关联的运行时对象Runtime rt = Runtime.getRuntime();System.out.println("处理器数量:"+ rt.availableProcessors());System.out.println("空闲内存数:"+ rt.freeMemory());System.out.println("总内存数:"+ rt.totalMemory());System.out.println("可用最大内存数:"+ rt.maxMemory());}}
Runtime类还有一个功能,它可以直接单独启动一个进程来运行操作系统的命令。
public class ExecTest{public static void main(String[] args)throws Exception{Runtime rt = Runtime.getRuntime();// 运行记事本程序rt.exec("notepad.exe");}}
阅读全文
0 0
- Java的System类,Runtime类
- Java中系统相关的类:System类、Runtime类
- Java---System类,RunTime类,Random类
- [Java 11] System 类 与 Runtime 类
- JAVA API系列----System与Runtime类
- System类、RunTime类
- Object/System/RunTime类
- Runtime和System类的操作例子
- Java的Runtime类
- java.lang.System类与java.lang.Runtime类
- java常用类库(StringBuffer类、Runtime类、System类)
- Java基础类库之Runtime类和System类
- 【java中常用工具类的总结】System类,Runtime类,Date类,Calendar类,Math类
- Java基础回顾 : Runtime类和System类
- System类及Runtime类
- System类和RunTime类
- System类和Runtime类
- Java基础<十四>---> 其他对象(System、Runtime、时间类、Math)
- webView不能跳转App Store
- 微信小程序 定位 获取经纬度城市街道等位置信息
- 你应该知道的PHP库
- 《统计学习方法笔记》——Logistic回归
- JS中数组的sort()排序
- Java的System类,Runtime类
- Flask扩展: Flask-Bootstrap
- ZooKeeper集群搭建 windows篇
- 金蝶EAS,KDTable数据填充,需重新解析表格tblMain.checkParsed()
- 安装mysql出现卡死
- win下使用realsense+opencv获取RGBD数据
- ADB server didn't ACK failed to start daemon
- SpringBoot——初接触
- python模块源代码加密方法