java System类的一些静态方法

package cn.sasa.demo2;

public class SystemDemo {
    public static void main(String[] args) {
        func_arraycopy();
    }
    
    static void func_1() {
        //currentTimeMillis() 获取当前的毫秒数 返回值long
        long time1 = System.currentTimeMillis();
        //System.out.println(time1);
        for(int i = 0; i<100000; i++) {
            System.out.println(i);//循环体里有内容才会执行循环
        }
        long time2 = System.currentTimeMillis();
        //System.out.println(time2);
        System.out.println(time2 - time1);
    }
    
    static void func_2() {
        //exit()  ---------终止程序
        while(true) {
            System.out.println("hi");
            System.exit(0);//终止程序
        }
        //System.out.println("hello"); //因为前面有exit(),程序不会运行这一步,编译报错
    }
    
    static void func_gc() {
        //gc 垃圾回收
        //当没有引用再指向该对象时,JVM会自动回收堆中的对象,同时调用该回收对象的finalize()方法
        new Person("sa",12);
        new Person("sa",12);
        new Person("sa",12);
        new Person("sa",12);
        new Person("sa",12);
        new Person("sa",12);
        new Person("sa",12);
        new Person("sa",12);
        System.gc();
    }
    
    static void func_3() {
        //System 类 getProperties 获取系统参数
        System.out.println(System.getProperties());
        System.out.println(System.getProperty("java.class.version"));
    }
    
    static void func_arraycopy() {
        //System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
        //src ----源数组
        //srcPos ----源数组的起始索引
        //dest ----目标数组
        //destPos ----目标数组的起始索引
        //length ----复制几个
        Person[] src = new Person[10];
        for(int i = 0; i<10; i++) {
            src[i] = new Person("sa",i);
        }
        
        Person[] dest = new Person[20];
        for(int i = 0; i<10; i++) {
            dest[i] = new Person("user",i);
        }
        
        System.arraycopy(src, 5, dest, 2, 3);
        for(int i = 0; i<dest.length; i++) {
            String pstr = dest[i].toString();
            System.out.println(pstr);
        }
    }
}
package cn.sasa.demo2;

public class Person {
    private String name;
    private int age;
    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return this.age;
    }
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void finalize() {
        System.out.println("测试gc ------ 垃圾回收了");
    }
    
    public String toString() {
        return "name:"+this.name +" age:"+this.age;
    }
}

 

posted @ 2018-12-17 10:55  SasaL  Views(394)  Comments(0Edit  收藏  举报