The system class represents the system. Many system-level attributes and control methods are placed inside the class. This class is located in the Java. lang package.
Because the constructor of this class is private, you cannot create an object of this class, that is, you cannot instantiate this class. The internal member variables and member methods are static, so they can be conveniently called.
1. member variables
The system class contains three member variables: In, out, and err, which represent the standard input stream (keyboard input), standard output stream (Display), and standard error output stream (Display ).
For example:
System. Out. println ("test ");
The function of this line of code is to output the string "test" to the system's standard output device, that is, to display it on the screen.
After I/O-related knowledge is learned, you can use the member methods in the system class to change the corresponding devices such as the standard input stream. For example, you can output the information output from the standard output stream to the file, to form log files.
2. Member Methods
The system class provides some system-level operation methods. The functions of these methods are as follows:
A. arraycopy Method
Public static void arraycopy (Object SRC, int srcpos, object DEST, int destpos, int length)
This method is used to copy an array, that is, copying the content in an array to a specified position in another array. Because this method is a native method, it is more efficient than loop.
Example:
Int [] A = {1, 2, 4 };
Int [] B = new int [5];
System. arraycopy (A, 1, B, 3, 2 );
The function of this Code is to copy array a from subscript 1 to the position where array B starts from subscript 3. A total of two copies are made. That is, copy a [1] to B [3] and copy a [2] to B [4]. After copying, the value in array A does not change, the value in array B is {0, 0, 0, 2, 3 }.
B. currenttimemillis Method
Public static long currenttimemillis ()
The function of this method is to return the current computer time. The expression format of time is the number of milliseconds between the current computer time and GMT time (Greenwich Mean Time) 00:00:00, January 1, January 1, 1970. For example:
Long L = system. currenttimemillis ();
The result is a long integer number, which is the current time expressed by the difference value.
The time obtained by this method is not intuitive enough, but it facilitates time calculation. For example, the following code can be used to calculate the time required for running the program:
Long start = system. currenttimemillis ();
For (INT I = 0; I <100000000; I ++ ){
Int A = 0;
}
Long end = system. currenttimemillis ();
Long time = end-start;
The value of the variable time indicates the number of milliseconds required for the For Loop execution in the Code. This method can be used to test the execution efficiency of programs with different algorithms, it can also be used for precise latency implementation during later thread control.
C. Exit Method
Public static void exit (INT status)
This method is used to exit the program. The value of status 0 indicates normal exit, and non-zero indicates abnormal exit. You can use this method to exit the program in graphic interface programming.
D. GC method
Public static void GC ()
This method is used to request the system to recycle garbage. Whether the system recycles immediately depends on the implementation of the garbage collection algorithm in the system and the execution of the system.
E. getproperty Method
Public static string getproperty (string key)
This method is used to obtain the value corresponding to the property named key in the system. The following table lists common attribute names and their functions.
Attribute name list
Attribute name |
Attribute description |
java.version
|
Java Runtime Environment version |
java.home
|
Java installation directory |
os.name
|
Operating system name |
os.version
|
Operating system version |
user.name
|
User Account Name |
user.home
|
User's home directory |
user.dir
|
Current working directory of the user |
For example:
String osname = system. getproperty ("OS. Name ");
String user = system. getproperty ("user. Name ");
System. Out. println ("the current operating system is:" + osname );
System. Out. println ("the current user is:" + User );
This method can be used to obtain many system-level parameters and corresponding values.