当前位置:  首页>> 技术小册>> Java语言基础9-常用API和常见算法

3.1.1 概述

  • System 类,定义在 java.lang 包中。
  • System 类中定义了一些常用的类字段和方法,该类不能实例化。

3.1.2 常用方法

  • 返回当前系统时间距离 1970-1-1 0:0:0 的毫秒值:
    1. public static native long currentTimeMillis();
    从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。常用于数组的插入和删除:
    1. public static native void arraycopy(Object src, int srcPos,
    2. Object dest, int destPos,
    3. int length);

运行垃圾回收器:

  1. public static void gc(){}

退出当前系统:

  1. public static void exit(int status){}

获取某个系统属性:

  1. public static String getProperty(String key){}

示例:

  1. package com.github.system.demo1;
  2. import java.util.Arrays;
  3. import java.util.Enumeration;
  4. import java.util.Properties;
  5. /**
  6. * @author maxiaoke.com
  7. * @version 1.0
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. // 返回当前系统时间距离1970-1-1 0:0:0的毫秒值:
  12. long current = System.currentTimeMillis();
  13. System.out.println("current = " + current);
  14. // 复制数组中的元素
  15. int[] arr = {1, 2, 3, 4, 5, 6};
  16. int[] target = new int[arr.length];
  17. System.arraycopy(arr, 0, target, 0, 2);
  18. System.out.println(Arrays.toString(target));
  19. // 获取所有系统属性
  20. Properties properties = System.getProperties();
  21. Enumeration<?> enumeration = properties.propertyNames();
  22. while (enumeration.hasMoreElements()) {
  23. Object o = enumeration.nextElement();
  24. System.out.println("o = " + o);
  25. }
  26. // 获取指定系统属性
  27. String os = System.getProperty("os.name");
  28. System.out.println("os = " + os);
  29. }
  30. }

该分类下的相关小册推荐: