`

System类,常用方法

    博客分类:
  • Java
阅读更多
public static long currentTimeMillis()
public class SystemDemo01{
	public static void main(String args[]){
		long startTime = System.currentTimeMillis() ;	// 取得开始计算之前的时间
		int sum = 0 ;			// 声明变量
		for(int i=0;i<30000000;i++){	// 执行累加操作
			sum += i ;
		}
		long endTime = System.currentTimeMillis() ;	// 取得计算之后的时间
		// 结束时间减去开始时间
		System.out.println("计算所花费的时间:" + (endTime-startTime) +"毫秒") ;
	}
};

public static Properties getProperties()
public class SystemDemo02{
	public static void main(String args[]){
		System.getProperties().list(System.out) ;	// 列出系统的全部属性
	}
};

public static String getProperty(String key)
public class SystemDemo03{
	public static void main(String args[]){
		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")) ;
	}
};

public static void gc()
解释:使用此方法必须覆写Object类中的
       public void finalize() throws Throwable
class Person{
	private String name ;
	private int age ;
	public Person(String name,int age){
		this.name = name ;
		this.age = age;
	}
	public String toString(){	// 覆写toString()方法
		return "姓名:" + this.name + ",年龄:" + this.age ;
	}
	public void finalize() throws Throwable{	// 对象释放空间时默认调用此方法
		System.out.println("对象被释放 --> " + this) ;
	}
};
public class SystemDemo04{
	public static void main(String args[]){
		Person per = new Person("张三",30) ;
		per = null ;	// 断开引用
		System.gc() ;		// 强制性释放空间
	}
};



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics