System.out.println()本身就是一个系统提供好的类。而且out.println()方法也是经常使用到的。
System类是一些与系统相关的属性和方法的集合。在System类中所有的属性都是静态的。
System类是一些与系统相关的属性和方法的集合,而且在System类中所有的属性都是静态的,想要引用这些属性和方法,直接使用System类调用即可。
public static void exit(int status) 系统退出,如果status为非0就表示退出
public static void gc() 运行垃圾收集机制,调用的是Runtime类中的gc方法
public static long currentTimeMillis() 返回以毫秒为单位的当前时间
public static void arraycopy(Objectssrc,intsrcPos,Object dest,int destPos,int length) 数组拷贝操作
public static Properties getProperties() 取得当前系统全部属性
public static String getProperty(String key) 根据键值取属性的具体内容
一个对象如果不使用,则肯定要等待进行垃圾收集,垃圾收集可以自动调用也可以手工调用,手工调用的时候就是调用System.gc()或者Runtime.getRuntime().gc()。但是,如果一个对象在回收之前需要做一些收尾的工作,则就必须覆写Object类中的:
protected void finalize() throws Throwable
在对象被回收之前进行调用,以处理对象回收前若干操作,例如释放资源等等。
System类本身提供的静态属性都是与IO操作有关的,可以使用System类取得计算的时间,以及通过gc()方法进行垃圾收集,此方法就是包装了Runtime类中的gc()方法。
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String toString(){
return "name:"+this.name+"\tage:"+age;
}
public void finalize()throws Throwable{
System.out.println("对象被释放--》"+this);
}
}
public class Main {
public static void main(String[] args) {
long start = System.currentTimeMillis();//取得系统当前时间,以毫秒为单位
int sum = 0;
for(int i = 0; i<3000000; i++){//运行一段相对时间较长的代码,为方便观看运行前后时间对比
sum += i;
}
long end = System.currentTimeMillis();//取得代码运行后的相对时间
System.out.println("start:"+start);
System.out.println("end:"+end);
System.out.println("计算花费时间:"+(end-start));
System.getProperties().list(System.out);
System.out.println("----------------------------------------");
System.out.println("系统版本:"+System.getProperty("os.name")
+System.getProperty("os.version")
+System.getProperty("os.arch"));
System.out.println("系统用户:"+System.getProperty("user.name"));
System.out.println("当前用户目录:"+System.getProperty("user.home"));
System.out.println("当前用户工作目录:"+System.getProperty("user.dir"));
System.out.println("----------------------------------------");
Person per = new Person("张三",22);
per = null;
System.gc();//强制收回 内存空间
}
}