The system class for Java common APIs

Source: Internet
Author: User

System, which represents the program, provides the corresponding system property information, and system operation. The system class cannot create objects manually because the constructor method is private and prevents objects from being created by the outside world. The system class is a static method, and the class name is accessed.

Common methods:

L currenttimemillis() Gets the millisecond difference between the current system time and the January 01, 1970 00:00 Point

L exit (int status) is used to end a running Java program. parameter to pass in a number. Typically incoming 0 is recorded as normal, others are in an abnormal state

The GC () is used to run the garbage collector in the JVM to complete the removal of garbage in memory.

L GetProperty (string key) is used to get system property information that is recorded in the specified key (string name)

The L arraycopy method, which is used to implement copying a portion of a source array to a specified location in the destination array

Method exercises for the system class:

L Practice One: Verify the time required to print the number 1-9999 for the For Loop (MS)

public static void Main (string[] args) {     long start = System.currenttimemillis (), for (int i=0; i<10000; i++) {         System.out.println (i);} Long end = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Total time-consuming milliseconds:" + (End-start));}

L Practice II: Copy the first 3 elements of the SRC array to the first 3 positions of the dest array

Before copying an element: src array element [1,2,3,4,5],dest array element [6,7,8,9,10]

After copying an element: src array element [1,2,3,4,5],dest array element [1,2,3,9,10]

public static void Main (string[] args) {int[] src = new int[]{1,2,3,4,5};int[] dest = new int[]{6,7,8,9,10}; System.arraycopy (src, 0, dest, 0, 3); After code runs: two elements in an array have changed src array element [1,2,3,4,5]dest array element [1,2,3,9,10]}

L Exercise three: loop to generate three digits between 100-999 and print that number, when the number can be divisible by 10, the end of the running program

public static void Main (string[] args) {     random random = new random (), while (true) {    int number = Random.nextint (900 ) +100; 0-899 +    if (nmumber% = = 0) {        system.exit (0);}}}

  

  

  

The system class for Java common APIs

Related Article

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.