System 类
常用方法
方法 描述 System.exit()退出当前程序 System.arraycopy(源数组,源数组起始索引,目标数组,目标数组起始索引,拷贝长度)复制数组元素,比较适合底层调用 System.currentTimeMillis()返回当前时间距离 1970-1-1 的毫秒数 System.gc()执行垃圾回收机制
一、exit()
传入参数 0 :表示正常退出
代码示例
public class main {
public static void main ( String [ ] args) {
System . out. println ( "hello world" ) ;
System . exit ( 0 ) ;
System . out. println ( "程序继续执行..." ) ;
}
}
hello world
二、System.arraycopy()
复制数组元素,比较适合底层调用
使用方法:System.arraycopy(源数组,源数组起始索引,目标数组,目标数组起始索引,拷贝长度)
代码示例
import java. util. Arrays ;
public class main {
public static void main ( String [ ] args) {
int [ ] arr = { 1 , 2 , 3 , 4 } ;
int [ ] arr_new = new int [ arr. length] ;
System . arraycopy ( arr, 0 , arr_new, 1 , 3 ) ;
System . out. println ( Arrays . toString ( arr_new) ) ;
}
}
[ 0 , 1 , 2 , 3 ]
代码说明
源数组:arr
源数组拷贝的起始位置:0
目标数组:arr_new
目标数组拷贝的起始位置:1
拷贝长度:3
三、System.currentTimeMillis()
返回当前时间距离 1970-1-1 的毫秒数
代码示例
public class main {
public static void main ( String [ ] args) {
System . out. println ( System . currentTimeMillis ( ) ) ;
}
}
1749372542217
四、System.gc()
主动触发 垃圾回收机制,底层会默认调用 finalize()方法执行垃圾回收机制
代码示例
public class finalize {
public static void main ( String [ ] args) {
finals finals = new finals ( 18 ) ;
finals = null ;
System . gc ( ) ;
}
}
class finals{
int age;
public finals ( ) {
}
public finals ( int age) {
this . age = age;
}
@Override
protected void finalize ( ) throws Throwable {
System . out. println ( "调用finalize回收对象" ) ;
}
}
调用finalize回收对象