System, which represents the program, provides the corresponding system property information, and system operation. The system class cannot create objects manually because the constructor method is private and prevents objects from being created by the outside world. The system class is a static method, and the class name is accessed.
Common methods:
L currenttimemillis() Gets the millisecond difference between the current system time and the January 01, 1970 00:00 Point
L exit (int status) is used to end a running Java program. parameter to pass in a number. Typically incoming 0 is recorded as normal, others are in an abnormal state
The GC () is used to run the garbage collector in the JVM to complete the removal of garbage in memory.
L GetProperty (string key) is used to get system property information that is recorded in the specified key (string name)
The L arraycopy method, which is used to implement copying a portion of a source array to a specified location in the destination array
Method exercises for the system class:
L Practice One: Verify the time required to print the number 1-9999 for the For Loop (MS)
public
static
void
main(String[] args) {
long
start = System.currentTimeMillis();
for
(
int
i=0; i<10000; i++) {
System.
out
.println(i);
}
long
end = System.currentTimeMillis();
System.
out
.println(
"共耗时毫秒:"
+ (end-start) );
}
L Practice II: Copy the first 3 elements of the SRC array to the first 3 positions of the dest array
Before copying an element: src array element [1,2,3,4,5],dest array element [6,7,8,9,10]
After copying an element: src array element [1,2,3,4,5],dest array element [1,2,3,9,10]
publicstatic voidmain(String[] args) {
int
[] src =
new
int
[]{1,2,3,4,5};
int
[] dest =
new
int
[]{6,7,8,9,10};
System.arraycopy( src, 0, dest, 0, 3);
代码运行后:两个数组中的元素发生了变化
src数组元素[1,2,3,4,5]
dest数组元素[1,2,3,9,10]
}
L Exercise three: loop to generate three digits between 100-999 and print that number, when the number can be divisible by 10, the end of the running program
| 123456789 |
publicstaticvoidmain(String[] args){ Random random = newRandom(); while(true){ intnumber = random.nextInt(900)+100; //0-899 + 100 if(nmumber % 10 == 0) { System.exit(0);}}} |
The system class for Java common APIs