[Learn JAVA from scratch | Part 21] Common API Introduction System

Table of contents

Foreword:

System:

Static method in System class:

Summarize:


Foreword:

                system is a very low-level API, a tool class that provides some system-related methods . He provided some very practical methods when we wrote the project. This article will introduce some practical system API method calls.

System:

The System class is a standard class provided by the Java language. It contains a series of static methods and member variables, which can be used to control the input, output, error output, time acquisition, system property acquisition, and program exit of Java programs. It is one of the most commonly used classes in Java programs.

Static method in System class:

1.out

System.out is a constant of the Java language standard output stream, which corresponds to the standard output device, and is often used to output data or log information to the console. System.out is an object of type PrintStream, which provides multiple methods for outputting data, such as print and println methods.

public class OutDemo {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

output:

Hello World!

2.err

System.err is the constant of the Java language standard error output stream, which corresponds to the standard error output device, and is often used to output error messages that occur during program operation.

public class ErrDemo {
    public static void main(String[] args) {
        System.err.println("Error occurred!");
    }
}

output:

Error occurred!

3.urrentTimeMillis

System.currentTimeMillis is a static method that returns the number of milliseconds since January 1, 1970 00:00:00 GMT, used for timing or getting the current time.

public class CurrentTimeDemo {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();

        // do some operation

        long endTime = System.currentTimeMillis();

        System.out.println("Time consumed: " + (endTime - startTime) + "ms");
    }
}

4.getProperty

System.getProperty is a static method that can be used to get system properties. Some commonly used system properties include operating system name, Java version number, file separator, system path separator, etc.

public class PropertyDemo {
    public static void main(String[] args) {
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("java.version"));
        System.out.println(System.getProperty("file.separator"));
        System.out.println(System.getProperty("path.separator"));
    }
}

output:

Windows 10
1.8.0_191
\
;

5.exit

System.exit is a static method that can be used to force exit the Java virtual machine. It accepts an integer parameter, which is the exit status code of the program, usually 0 means normal exit, non-zero means abnormal exit.

public class ExitDemo {
    public static void main(String[] args) {
        System.exit(0);
    }
}

6. arraycopy

System.arraycopy is a static method used to copy the contents of one array into another array. It accepts five parameters, which are the source array, the starting position of the source array, the target array, the starting position of the target array, and the number of elements to be copied.

public class ArrayCopyDemo {
    public static void main(String[] args) {
        int[] srcArr = {1, 2, 3, 4, 5};
        int[] destArr = new int[5];

        System.arraycopy(srcArr, 0, destArr, 0, 5);

        System.out.println(Arrays.toString(destArr));
    }
}

output:

[1, 2, 3, 4, 5]

7. gc

System.gc is a static method that can be used to request the Java virtual machine to perform garbage collection operations. Calling this method does not guarantee that garbage collection will be executed, it just sends a hint to the virtual machine.

public class GCDemo {
    public static void main(String[] args) {
        Object obj = new Object();

        obj = null;
        System.gc();
    }
}

8. setIn,setOut,setErr

The System class also provides three static methods setIn, setOut and setErr, which can be used to modify standard input, output and error output devices. These methods can be used to redirect program input and output streams to special devices such as files or networks.

public class RedirectDemo {
    public static void main(String[] args) throws FileNotFoundException {
        System.setOut(new PrintStream(new FileOutputStream("output.txt")));
        System.out.println("Hello World!");
    }
}

Summarize:

        The above are several static methods and usage examples commonly used in the System class. The system class also provides some other APIs, such as the identityHashCode method can be used to obtain the hash code of the object, the getenv method can be used to obtain environment variables, and so on. Mastering the usage of these APIs can make Java programming more efficient and convenient.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

Guess you like

Origin blog.csdn.net/fckbb/article/details/131406947