Java—System类和Runtime类

System类

System类介绍

  System类表明Java程序运行平台,程序不能建立该对象,可是System类提供了直接调用的类方法和类变量。
  System类提供标准输入、标准输出、错误输出的类变量;且提供访问环境变量、系统属性、系统时间等静态方法。java

System类用法

环境变量和系统属性

public static void main(String[] args) throws Exception {
  //获取全部的系统环境变量
  Map<String, String> env = System.getenv();
  for (String envName : env.keySet()) {
    System.out.printlin(envName + " : " + env.get(envName));
  }
  //获取指定环境变量
  System.out.printlin(System.getenv("JAVA_HOME"));
  //获取全部的系统属性
  Properties properties = System.getProperties();
  //将系统属性保存到文件中
  properties.store(new FileOutputStream("properties.txt"), "System Properties");
  //获取指定的系统属性
  System.out.println(System.getProperty("os.name"));
}

系统时间

  System类获取系统当前时间的方法:
currentTimeMillis():返回一个long型整数,返回当前时间与UTC 1970年1月1日午夜的时间差,以毫秒为单位。
nanoTime():返回一个long型整数,返回当前时间与UTC 1970年1月1日午夜的时间差,以纳秒为单位。ide

hash值

  System类提供返回指定对象精确hash值的方法。
identityHashCode(Object obj):根据对象的地址计算出hashCode值,若两个对象的identityHashCode值相同,则两个对象相同。code

Runtime类

Runtime类介绍

  Runtime类表明Java程序的运行环境,每一个Java程序有一个与之对应的Runtime实例,应用程序经过该对象与其运行时环境相连,应用程序不能够建立本身的Runtime实例,可经过getRuntime()方法获取与之关联的Runtime对象。
  Runtime类提供load(String filename)loadLibrary(String libname)方法加载文件和动态连接库。对象

相关文章
相关标签/搜索