Java System class method outlined and its members

System class

  • System Class Overview
System class contains several useful class fields and methods. It can not be instantiated.
  • Member method
static void public gc (): Runs the garbage collector
public static void exit( int status )
public static long currentTimeMillis ()
public static void arraycopy (Object src,int srcPos,Object dest,int destPos,int length)

 

 

1, public static void gc (): Runs the garbage collector.

package cn.wen_01;


public class SystemDemo {
	public static void main(String[] args) {
		
        Person p = new Person("小明", 10);
		System.out.println(p);

		p = null; // 让p不再指定堆内存
		System.gc();
	}
}
package cn.wen_01;

public class Person {
	private String name;
	private int age;

	public Person() {
		super();
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}

	@Override
	protected void finalize() throws Throwable {
		System.out.println("当前的对象被回收了" + this);
		super.finalize();
	}

}

System.gc () can be used for garbage collection. When using the System.gc () before the memory occupied by an object recycling, call the appropriate method of claim procedures to clean up resources. In the absence of clearly specified resource cleanup, the Java enhance the default mechanism to clean up the resources of the object, it is to call the Object class finalize () method. finalize () when the effect of the method is to release the memory space occupied by an object, is JVM called. The subclasses override this method, you can clean up resources occupied by the object, which has no chain call, it must be implemented manually.

It is found from the results of running the program, execution System.gc () before, the system will automatically call the finalize () method clears the resource object occupied by super.finalize () mode can be achieved from the bottom to the finalize () method call , which is to release its own resources, go to release the resources of the parent class.

But do not call frequently garbage collection in the program, because every time the garbage collection, the JVM will be forced to start the garbage collector runs, it will consume more system resources, will be the normal Java competition for resources running only in perform a large number of the release of the object, it is best to call the garbage collector. (To understand)

 

2, public static void Exit (int Status): Terminates the currently running Java Virtual Machine. Parameters used as a status code;

      By convention, the non-zero status code indicates abnormal termination.

. 3, public static Long with currentTimeMillis (): returns the current time in milliseconds

Both methods test code as follows:

package cn.wen_02;

public class SystemDemo {
	public static void main(String[] args) {
		// System.out.println("小东");
		// System.exit(0);
		// System.out.println("小明");    //不执行

		// System.out.println(System.currentTimeMillis());

		// 单独得到这样的实际目前对我们来说意义不大
		// 那么,它到底有什么作用呢?
		// 要求:统计这段程序的运行时间

		long start = System.currentTimeMillis();
		for (int x = 0; x < 100000; x++) {
			System.out.println("hello" + x); 
		}
		long end = System.currentTimeMillis();
		System.out.println("共耗时:" + (end - start) + "毫秒");
	}
}

4, public static void arraycopy (Object
src, int srcPos, Object dest, int destPos, int length)      Copies an array from the specified source array, beginning at the specified location, the end of the array to the target location specified

package cn.wen_03;

import java.util.Arrays;

public class SystemDemo {
	public static void main(String[] args) {
		// 定义数组
		int[] arr = { 11, 22, 33, 44, 55 };
		int[] arr2 = { 6, 7, 8, 9, 10 };

		// 请大家看这个代码的意思
		System.arraycopy(arr, 1, arr2, 2, 2);

		System.out.println(Arrays.toString(arr));    //11, 22, 33, 44, 55
		System.out.println(Arrays.toString(arr2));    //6, 7, 22, 33, 10
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 91 original articles · won praise 16 · views 1184

Guess you like

Origin blog.csdn.net/hewenqing1/article/details/103840369