Introduction to System classes in Java

Source: Internet
Author: User

The last interview encountered a question, asked the System.out.println () out is not the internal class "is not an internal class", then asked, the intuitive feeling out should be a property of the system class, what is the relationship with the inner class? And before finishing IO part of the time remember there is a PrintStream class for standard output, but never looked at the system source code, also dare not casually say. Later read the next source, found that is really printstream, may want to ask is the use of internal class (really feel the interview to rely on fate, a lot of interviewers like to guide the question, the way is very good, but many times may let the interviewer can not clear what you want to ask, I this interview is deeply hit, To the last interviewer each question I have to think for a long while is not to leave what pit), but in the final analysis of their level is much worse, or to seriously study. To get to the next, the System class is a tool class provided by the JDK, which has a final modification, is not inheritable, and can be seen by the name, where most of the operations are related to the system. The main functions are as follows:

  • Standard input output, such as out, in, err
  • Externally defined properties and access to environment variables, such as getenv ()/setenv () and GetProperties ()/setproperties ()
  • Methods for loading files and class libraries, such as load () and LoadLibrary (),
  • A quick way to copy an array: Arraycopy ()
  • Some JVM operations, such as GC (), Runfinalization (), exit (), are not mentioned in the source Java doc, possibly because they are not recommended for proactive invocation. And these methods are just runtime.getruntime () call, the two do not distinguish the bottom of the direct view, the main methods and functions are listed. Below we focus on how the class is initialized. First, at the beginning, we can look at the following code:

    private static native void registerNatives();static { registerNatives();}

    The static code block in the class calls a native method Registernatives (), which can be guessed that the method should be an ingress method, and take a look at the comment: by statically initializing the Register native method, This method causes the VM to complete the initialization work by calling the Initializesystemclass method. Sure enough, then let's look at the Initializesystemclass method:

    PrivateStaticvoidInitializesystemclass() {Initialize Props props =New Properties (); InitProperties (props); Sun.misc.VM.saveAndRemoveProperties (props);Gets the system-related newline character lineseparator = Props.getproperty ("Line.separator"); Sun.misc.Version.init ();Create an instance object of in, out, err, respectively, and initialize with setXX0 () to view the SetXX0 () method, which is a native method that manages the system's standard flow to objects within the class FileInputStream Fdin =New FileInputStream (FileDescriptor.in); FileOutputStream fdout =New FileOutputStream (FileDescriptor.Out); FileOutputStream Fderr =New FileOutputStream (FILEDESCRIPTOR.ERR); SetIn0 (New Bufferedinputstream (Fdin)); SetOut0 (New PrintStream (New Bufferedoutputstream (Fdout,128),true)); SetErr0 (new printstream (new bufferedoutputstream (FDERR, Span class= "Hljs-number" >128), true)); //load the zip package to get java.util.zip.ZipFile this class so that after loading Licoux use LoadLibrary (  "zip"); //set the platform-related signal processing terminator.setup (); //initialize SUN.MISC related environment variables sun.misc.VM.initializeOSEnvironment (); //the main thread does not add the same threads to the same thread group, we must implement it ourselves here. Note Half a day did not understand, look at the code is the main thread himself added to their own thread group ... Thread current = Thread.CurrentThread (); Current.getthreadgroup (). Add (current); //register a shared secret key? Note is not clear, the method is to instantiate a javalangaccess setjavalangaccess (); The //subsystem can invoke sun.misc.VM.isBooted () at initialization time to ensure that nothing is done before the application class loader starts. Booted () is actually changing the state so that isbooted () becomes true. Sun.misc.VM.booted ();}            

    At this point, the system is basically finished, but there are a lot of low-level things do not understand (setjavalangaccess ()), to be resolved later.

Http://www.cnblogs.com/good-temper/p/5042058.html

Introduction to System Classes in Java (GO)

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.