Java—System类和Runtime类

System类

System类介绍

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

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日午夜的时间差,以纳秒为单位。

hash值

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

Runtime类

Runtime类介绍

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

猜你喜欢

转载自www.cnblogs.com/Andya/p/12445280.html
今日推荐