系统类——System类

System类的几大功能:

  • 访问系统属性
  • 访问环境变量
  • 标准输入和输出
  • 加载文件和动态链接库
  • 复制数组
  • 系统退出命令
  • 执行垃圾回收

常用属性方法:
这里写图片描述

import java.util.Properties;
class Person1 {
    private String name;
    private int age;
    public Person1(String name,int age) {
        this.name  = name;
        this.age = age;
    }
    public String toString() {
        return ("姓名:"+name+",年龄"+age);
    }
    public void finalize() throws Throwable {
        System.out.println("释放对象"+this);
        super.finalize();
    }
}
/**
 * System类
 * @author lzq
 *
 */
public class TestDemo2 {

    /**
     * 获取系统属性
     */
    public static void show() {
        Properties pr = System.getProperties(); //获取系统属性
        pr.list(System.out);                    //列出属性
    }
    /**
     * 复制数组
     * 
     */
    public static void show1() {
        char[] src = {'A','B','C','D'};
        char[] dest = {'1','2','3','4'};
        System.arraycopy(src, 2, dest, 1, 2);
        for(char array:dest) {
            System.out.print(array+" ");
        }

    }
    /**
     * 计算程序执行时间
     */
    public static void show2() {
        long start = System.currentTimeMillis();
        System.out.println("开始时间:"+start);
        long rus = 0;
        for(long i = 1;i < 88888888L;i++) {
            rus = rus+i;
        }
        long end = System.currentTimeMillis();
        System.out.println("结束时间:"+end);
        System.out.println("运行时间:"+(end-start));
    }
    /**
     * 垃圾对象的回收
     */
    public static void show3() {
        Person1 p = new Person1("小强",21);
        System.out.println(p);
        p = null;
        System.gc();
    }
    public static void main(String[] args) {
        show();
        show1();
        show2();
        show3();
    }

}

猜你喜欢

转载自blog.csdn.net/QQ2899349953/article/details/81660177