03.System类的概述和常用方法

System类

System 类包含一些有用的类字段和方法。它不能被实例化。

成员方法

  • static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) :
    • 从src源数组的srcPos索引开始,复制length个元素
    • 从destPost位置开始将这些元素放至到dest数组中
  • static long currentTimeMillis()
    • 返回以毫秒为单位的当前时间
  • static void exit(int status)
    • 终止当前正在运行的 Java 虚拟机
  • static void gc()
    • 运行垃圾回收器
package com.itheima_02;
/*
 *  System:包含一些有用的类字段和方法。它不能被实例化。
 *      static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
 *      static long currentTimeMillis()    
 *      static void exit(int status) 
 *      static void gc()  
 */
public class SystemDemo {
    public static void main(String[] args) {
//      method();
//      method2();
//      method3();
        //static void gc():运行垃圾回收器
        new Demo();
        System.gc();
    }

    private static void method3() {
        //static void exit(int status) :终止正在运行的虚拟机
        
        for (int i = 0; i < 100000; i++) {
            System.out.println(i);
            if(i == 100) {
                System.exit(0);
            }   
        }
    }

    private static void method2() {
        /*
         * static long currentTimeMillis():以毫秒值返回当前系统时间
         *  这个毫秒的时间是相对时间,相对于1970-1-1 00:00:00   :0
         *  1970-1-1    00:00:01:1000
         *  1970-1-1    00:01:00:1000 * 60
         *  1970-1-1    01:00:00:1000 * 60 * 60
         *  1000毫秒 = 1秒
         */
//      System.out.println(System.currentTimeMillis());
        
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    private static void method() {
        /*
         * static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
         *  复制数组
         *  参数1:源数组
         *  参数2:源数组的起始索引位置
         *  参数3:目标数组
         *  参数4:目标数组的起始索引位置
         *  参数5:指定接受的元素个数
         */
        int[] src = {1,2,3,4,5};
        int[] dest = new int[5];
        System.arraycopy(src, 2, dest, 0, 3);
        
        for(int i = 0; i < dest.length; i++) {
            System.out.print(dest[i]);
        }
    }
}

class Demo {
    @Override
    protected void finalize() throws Throwable {
        System.out.println("我被回收了");
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第05天API 今日内容介绍 ·Object类& System类 ·日期相关类 ·包装类&正则表达式 ·Date对...
    chcvn阅读 3,269评论 0 1
  • 朋友小A最近一直在微信上找我,刚开始她兴致不高的样子让我一度以为她遭遇了天崩地裂的事情,在她的梨花带雨与我的耐心劝...
    红线婚恋阅读 1,499评论 0 0
  • 我曾经爱过你 爱情,也许在我的心灵里还没有完全消亡 但愿,它不会再打扰你 我曾经默默无语地,毫无指望地爱过你 我既...
    六月旬阅读 1,778评论 0 0
  • (一)、体系性强、内容丰富 RAZ起源于美国、加拿大等发达国家,在香港、台湾地区发展了十几年,是根据儿童不...
    EP_Lily阅读 5,522评论 0 0

友情链接更多精彩内容