java API -- System类

System类是我们一开始学习java就接触到的一个常用类,在这个类中封装了许多和系统底层有关的方法供我们调用。

所属包

首先,查看jdk我们可以知道,System类是位于java.lang包下的。因此,是无需导包就能直接使用的。

类描述

我们先看一段官方注解:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* The <code>System</code> class contains several useful class fields
* and methods. It cannot be instantiated.
*
* <p>Among the facilities provided by the <code>System</code> class
* are standard input, standard output, and error output streams;
* access to externally defined properties and environment
* variables; a means of loading files and libraries; and a utility
* method for quickly copying a portion of an array.
*
* @author unascribed
* @since JDK1.0
*/

大意就是,System这个类一个基础工具类,它无法被实例化。

它主要用于标准输入、输出流的控制,以及数组的操作等。

构造函数

1
2
3
/** Don't let anyone instantiate this class */
private System() {
}

由于构造函数的私有属性,因此System类是无法实例化的。

常用成员方法

输出方法

这里介绍两个方法,分别是:System.out.println()以及System.err.println()

实际上,这两个方法都是通过System内的静态不可变成员变量outerr,并且它们都是PrintStream类的的对象引用,然后通过对象调用类中方法。

数组操作方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
* @param      src      the source array.
* @param srcPos starting position in the source array.
* @param dest the destination array.
* @param destPos starting position in the destination data.
* @param length the number of array elements to be copied.
* @exception IndexOutOfBoundsException if copying would cause
* access of data outside array bounds.
* @exception ArrayStoreException if an element in the <code>src</code>
* array could not be stored into the <code>dest</code> array
* because of a type mismatch.
* @exception NullPointerException if either <code>src</code> or
* <code>dest</code> is <code>null</code>.
*/
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);

从源数组src向目的数组dest,复制给定长度length的数据。

退出jvm方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Terminates the currently running Java Virtual Machine. The
* argument serves as a status code; by convention, a nonzero status
* code indicates abnormal termination.
* <p>
* This method calls the <code>exit</code> method in class
* <code>Runtime</code>. This method never returns normally.
* <p>
* The call <code>System.exit(n)</code> is effectively equivalent to
* the call:
* <blockquote><pre>
* Runtime.getRuntime().exit(n)
* </pre></blockquote>
*
* @param status exit status.
* @throws SecurityException
* if a security manager exists and its <code>checkExit</code>
* method doesn't allow exit with the specified status.
* @see java.lang.Runtime#exit(int)
*/
public static void exit(int status) {
Runtime.getRuntime().exit(status);
}

方法描述为:终止正在运行的jvm,并返回一个状态码。依照惯例:返回‘0’,代表正常退出 ;其他值,代表异常退出。

垃圾回收方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Runs the garbage collector.
* <p>
* Calling the <code>gc</code> method suggests that the Java Virtual
* Machine expend effort toward recycling unused objects in order to
* make the memory they currently occupy available for quick reuse.
* When control returns from the method call, the Java Virtual
* Machine has made a best effort to reclaim space from all discarded
* objects.
* <p>
* The call <code>System.gc()</code> is effectively equivalent to the
* call:
* <blockquote><pre>
* Runtime.getRuntime().gc()
* </pre></blockquote>
*
* @see java.lang.Runtime#gc()
*/
public static void gc() {
Runtime.getRuntime().gc();
}

调用jvm的垃圾回收机制,去尽可能的回收系统资源。一般与Object类的finalize()方法配套使用

1
2
3
4
5
6
7
/**
* Called by the garbage collector on an object when garbage collection
* determines that there are no more references to the object.
* A subclass overrides the {@code finalize} method to dispose of
* system resources or to perform other cleanup.
* <p>
protected void finalize() throws Throwable { }

流程是:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Test{
public static void main(String[] args){
new Student(); //创建匿名对象,方便gc回收
System.gc(); //调用jvm回收机制
}
}

class Student{
@override
void finalize(){
System.out.println("在这里完成一些收尾工作");
}
}

运行结果是:可能打印出“在这里完成一些收尾工作”。因为gc不能保证每次都能回收Student资源(the Java Virtual Machine has made a best effort to reclaim space from all discarded)。