Java API Getting Started Tutorial Java System class

Source: Internet
Author: User

System classes represent systems, and many of the system-level properties 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 that class, that is, you cannot instantiate the class. Its internal member variables and member methods are static, so it can be easily called.

1. Member variables

The system class contains in, out, and err three member variables representing the standard input stream (keyboard input), the standard output stream (display), and the standard error output stream (display).

For example:

System.out.println ("Test");

The purpose of this line of code is to output the string "Test" to the standard output device of the system, which is displayed on the screen.

Subsequent to the completion of the IO-related knowledge, you can use the System class member methods to change the standard input stream and other corresponding devices, such as the output of the standard output stream can be output to the inside of the file, so as to form a log file.

2. Member Methods

System-level operations are provided in the systems class, which are implemented in the following ways:

A, Arraycopy method

public static void Arraycopy (object src, int srcpos, object dest, int destpos, int length)

The function of this method is to copy the array, that is, the contents of one array to the specified position in another array, because the method is the native method, so the performance is more efficient than the use of loops.

Examples of Use:

int [] A = {1,2,3,4}; int New int [5]; System.arraycopy (A,1,b,3,2);

The purpose of this code is to copy the array A, starting with the subscript 1, and copying it to the position where array B starts at subscript 3, copying 2 altogether. That is, copy a[1] to b[3], copy a[2] to b[4], so that the value in array A is not changed after copying, and the value in array B becomes {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 time expression format is the current computer time and GMT time (GMT) January 1, 1970 0:0 0 seconds the number of milliseconds.

For example:

long L = System. Currenttimemillis ();

You will get a long integer number, which is the current time expressed in the difference value. The time used for this method is not intuitive, but it is convenient to calculate the time.

For example, you can use the following code to calculate the time it takes for a program to run:

long start =for (int i = 0;i < 100000000;i++) {         int a = 0   long end =long time = end-start; System.out.println (time);

The value of the variable time here represents the number of milliseconds required for the For loop execution in the middle of the code, which can be used to test the execution efficiency of programs of different algorithms, and also for the precise delay implementation of post-threading control.

C, Exit method

public static void exit (int status)

The function of this method is to exit the program. Where status has a value of 0 for normal exit, not 0 for abnormal exit. By using this method, the program Exit function can be realized in the programming of graphical interface.

D, GC method

public static void GC ()

The function of this method is to request the system for garbage collection. Whether the system is recovered immediately depends on the implementation of the garbage collection algorithm in the system and the implementation of the system.

E, GetProperty method

public static string GetProperty (String key)

The function of this method is to obtain the value corresponding to the attribute named key in the system. The property names and properties that are common in the system are shown in the following table.

List of property names

Property name

Property Description

java.version

Java Runtime Environment version

java.home

Java installation directory

os.name

Name of the operating system

os.version

Version of the operating system

user.name

User's account name

user.home

User's home directory

user.dir

User's current working directory

For example:

String osname = System.getproperty ("os.name"= System.getproperty ("User.Name"); System.out.println ("Current operating system is:" + osname); System.out.println ("Current user is:" + user);

Many system-level parameters and corresponding values can be obtained by using this method.

Refer to this article specifically:

get system Information through Java's runtime.getruntime () and system.getproperties ()

Java API Getting Started Tutorial Java System class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.