Java Common Object System Class-30-Basic Understanding and Several Common Member Methods

       In this article, let’s learn about the System class. We have used this class in at least two previous places. One is System.out.println(), which writes output statements every day, and the other is to receive the keyboard during the use of the Scanner class. Enter the parameter System.in of the object. After learning this, you will be able to understand the approximate role of System.in and System.out.

1. Basic Features

      Open the JAVA API document, search to find the System class, you can understand that the System class is under the lang package, so you do not need to import the package to use System. All member methods under the System class are decorated with static, and when you look at the source code of the System class and find the constructor, they are decorated with private, so the System class cannot be created with new, and a class object cannot be instantiated. In the field area, you can see that err represents the standard error output stream, in represents the standard input stream, which generally refers to the keyboard, and out represents the standard output stream, which is generally output to the console. So, the print statement we've been outputting is a method of the standard output stream.

2. Member method practice

1)gc()

       This method is to call the garbage collection method. The Object class has a finalize() method. Generally speaking, the system determines when the garbage collection is reached, and then the finalize() method is called to handle the garbage collection. If the programmer wants to handle garbage collection automatically, he can call the finalize() method to perform garbage collection through the System.gc() method. About this method, it is easy to understand.

 

2)exit(int status)

       The role of this method is to terminate the currently running JAVA virtual machine. The parameter status is an int type, 0 means exit normally, non-0 means exit the java virtual machine abnormally.

package otherclass;

public class Demo1_System {

   public static void main(String[] args) {

     System.out.println("Start");
     System.exit(0);
     System.out.println("end");
   }
}

As you can see, only Start is output, and end is not, because the previous line performed the exit JVM operation.


3)currentTimeMillis()

       Returns the current time in milliseconds, and sometimes we can convert this time into a time in a different format to do other things, such as log output, and timestamps may be used.

package otherclass;

public class Demo1_System {

   public static void main(String[] args) {

     intsum = 0;

     longtime_start = System.currentTimeMillis();

     for (inti = 0; i <= 100000; i ++) {

        sum += i;

     }

     System.out.println(sum);

     longtime_end = System.currentTimeMillis();

     System.out.println(time_end - time_start);

   }
 
}

       The result of my computer is 2 milliseconds. This calculation performance is relatively good, because one second is equal to 1000 milliseconds, and the calculation of 2 milliseconds adds up from 1 to 100,000, which is very fast.

 

4)arraycopy()

Array copy, copy a new array with the same length and content from the original array.

package otherclass;

import java.util.Arrays;

public class Demo1_System {

   public static void main(String[] args) {

     int[] src = {11,22,33,44,55}; //5 elements

     int[] dest = newint[8]; //8 elements

     / / Through the arraycopy method, the complete array is copied to the new array

     System.arraycopy(src, 0, dest, 0, 5);

     for(inti=0; i < dest.length; i++) {

        System.out.print(dest[i]+ " ");

     }

   }

}

Output: 11 22 33 44 55 0 0 0

 

Adjust the start, end and length parameters, and then use this array copy method.

package otherclass;

import java.util.Arrays;

public class Demo1_System {

   public static void main(String[] args) {

     int[] src = {11,22,33,44,55}; //5 elements

     int[] dest = newint[8]; //8 elements

     / / Through the arraycopy method, the complete array is copied to the new array

     System.arraycopy(src, 1, dest, 4, 3);

     for(inti=0; i < dest.length; i++) {

        System.out.print(dest[i]+ " ");
     }

   }

}

 

Output: 0 0 0 0 22 33 44 0

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324809473&siteId=291194637