Tiantian said to use System.out.println for output, so I have a small question to ask, is out a variable or an internal class? Large and systematic knowledge is explained in detail in various topics. We cannot ignore these scattered knowledge points, otherwise it will be very embarrassing if we are asked such a simple question during the interview and cannot answer it.
Non-instantiable System class
System is a system class. In the java.lang package of JDK, it can be seen that it is also a core language of java. characteristic. The constructor of the System class is decorated with private and is not allowed to be instantiated. Therefore, the methods in the class are also static modified static methods.
Field
public final static InputStream in;
//标准输入流
public final static PrintStream out;
//标准输出流
public final static PrintStream err;
//标准错误流
Copy after login
It can be seen that out and in in System are not internal classes, but genuine field variables. out is the variable field modified by PrintStream's final static, which means it can call methods of the PrintStream class. Println is an output method of PrintStream, so we usually use System.out.println() to output content on the console.
The arraycopy method has five parameters, which are the array to be copied, the starting position to be copied, the array to copy to, the starting position of the array to copy, and the copy to the end of this array. This method is similar to copyOf and copyOfRange in Arrays. It has more parameters and can be used if necessary.
currentTimeMillis——Return the number of milliseconds
I won’t give an example here, the currentTimeMillis method and the getTime method in the Date class It's exactly the same, if you just need milliseconds, such a call is also very convenient. However, it should be noted that currentTimeMillis does not directly get the result of getTime. currentTimeMillis is a local method that returns the time of the operating system. Since the minimum accuracy of the time of some operating systems is 10 milliseconds, this method may cause some deviations. .
/* register the natives via the static initializer.
*
* VM will invoke the initializeSystemClass method to complete
* the initialization for this class separated from clinit.
* Note that to use properties set by the VM, see the constraints
* described in the initializeSystemClass method.
*/
private static native void registerNatives();
static {
registerNatives();
}
Copy after login
native不用看了,本机方法。这是可以猜得到的,因为System类要使用输入和输出流可能会用到和操作系统相关的一些本机方法。那么在static块中调用了registerNatives()方法,这个方法是本地方法我们看不到具体实现。但是注释说了:“VM will invoke the initializeSystemClass method to complete the initialization for this class separated from clinit”。
那么JVM调用的initializeSystemClass方法是怎么实现的呢?
private static void initializeSystemClass() {
props = new Properties();
initProperties(props);
sun.misc.VM.saveAndRemoveProperties(props);
lineSeparator = props.getProperty("line.separator");
sun.misc.Version.init();
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn));
setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
loadLibrary("zip");
Terminator.setup();
sun.misc.VM.initializeOSEnvironment();
Thread current = Thread.currentThread();
current.getThreadGroup().add(current);
setJavaLangAccess();
sun.misc.VM.booted();
}
The above is the detailed content of In-depth analysis of java-System system class. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn