1. System class overviewThe System class contains some useful class fields and methods. It cannot be instantiated.2. Member MethodsPublic static void GC (): Running the garbage collector System.GC () is available for garbage collection. When using System.GC () to reclaim memory occupied by an object, the resource is cleaned by requiring the program to call the appropriate method. In the absence of explicitly specified resource cleanup, Java improves the default mechanism to clean up resources for the object, which is called the Finalize () method of the object class. The Finalize () method is called by the JVM when it frees up memory space occupied by an object. By overriding the method, the subclass can clean up the resource used by the object, which has no chain call, so it must be implemented manually. From the running results of the program can be found, before executing System.GC (), the system will automatically call the Finalize () method to clear the object occupies the resources, through the super.finalize () way can be implemented from the bottom to the last Finalize () method call, That is, releasing your own resources before releasing the resources of the parent class. However, do not make frequent calls to garbage collection in your program, because each time a garbage collection is performed, the JVM forces the garbage collector to run, which consumes more system resources, competes with the normal Java program, and only calls garbage collection if it is performing a large number of objects.Public static void exit (int status): Terminates the currently running Java virtual machine. The parameter is used as a status code; By convention, a status code other than 0 indicates an abnormal termination.Public Static Long Currenttimemillis (): Returns the current time in millisecondsPublic static void Arraycopy (Object src,int srcpos,object dest,int destpos,int length): Note that the naming is wrong, but it needs to be remembered. Copies an array from the specified source array, starting at the specified position, ending at the specified position in the destination array, and paying attention to replacing instead of inserting.Example 1:
Public classPerson {Private intAge ; PrivateString name; PublicPerson (intAge , String name) { This. Age =Age ; This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"person{" + "age=" + Age + ", name= '" + name + ' \ ' + '} '; } @Overrideprotected voidFinalize ()throwsthrowable {System.out.println ("The current object is recycled," + This); Super. Finalize (); }}
Public class SystemDemo01 { publicstaticvoid main (string[] args) { new person (60, "Gigi"); SYSTEM.OUT.PRINTLN (p); NULL ; System.GC (); }}
Output Result:person{age=60, name= ' Gigi '} The current object is recycled, person{age=60, name= ' Gigi '} Example 2:
Public class SystemDemo02 { publicstaticvoid main (string[] args) { System.out.println ("I love China"); System.exit (0); System.out.println ("I Love You");} }
Output Result:I Love China Example 3:
Public class SystemDemo03 { publicstaticvoid main (string[] args) { Long start = system.currenttimemillis (); for (int x = 0; x < 100000; + +) { System.out.println ("Hello:" + x); } long end = system.currenttimemillis (); System.out.println ("Total time:" + (End-start) + "millisecond");} }
Output Result:.... hello:99999 total time: 2680 msExample 4:
Public class SystemDemo04 { publicstaticvoid main (string[] args) { int[] arr1 = {11,22,33,44,55}; int [] arr2 = {5,6,7,8,9,10}; System.arraycopy (arr1,1,arr2,2,2); System.out.println (arrays.tostring (arr1)); System.out.println (arrays.tostring (ARR2));} }
Output Result:[11, 22, 33, 44, 55] [5, 6, 22, 33, 9, 10]
Java Api--system Class