《第一行代码-李兴华》-10.3 System类

10.3 System类

视频地址System类

/**
 * 
 * 统计某项操作执行时间
 *
 */
long start = System.currentTimeMillis();//返回以毫秒为单位的当前时间。
  String str ="";
  for(int i=0;i<10000;i++) {
   str +=i;
  }
  long end = System.currentTimeMillis();
  System.out.println("本次操作用时"+(end-start));  

class Devil{
 public Devil() {
  System.out.println("天崩地裂,魔鬼出世");
 }
 @Override
 protected void finalize() throws Throwable {//对象回收方法
  System.out.println("魔鬼死了,全世界庆贺!!");
  throw new Exception("老子下个世纪还要祸害地球!于是嗝屁了");
 }
}
public class TestDemo {
 public static void main(String args[]) throws Exception {
  Devil devil = new Devil();  //实例化新对象
  devil=null;      //产生垃圾
  System.gc();     //手工处理垃圾收
 }
}
面试题

请解释final、finally、finallize的区别

  • final:关键字,定义不能被继承的类、不能被覆写的方法、常量;
  • finally:关键字,异常的统一出口;
  • finallize:方法,Object类提供的方法(protected void finalize() throws Throwable)即使出现异常也不会中断
总结
  1. System类可以使用currentTimeMillis()方法取得当前系统时间;
  2. System类gc()方法直接调用“Runtime.getRuntime().gc()”方法
posted @ 2018-02-22 11:19  一粒飞鸿  阅读(163)  评论(0编辑  收藏  举报