Java->system class

Source: Internet
Author: User

Concept

The system class introduced in the API is relatively simple, we give the definition, systems in which the program is located, provide some of 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 . In the JDK, there are many such classes.

Common Methods

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

 exit (int status) 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

 GC () used to run the garbage collector in the JVM to complete the cleanup of in-memory garbage.

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

 System.arraycopy (the original array, the starting position of the original array, the new array, the starting position of the new array, the length of the new array); method that implements the copy of the source array part element to the specified location of 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 the code has run: two elements in the array have changed

src array element [1,2,3,4,5]

Dest array elements [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 (+100);//0-899 +

if (nmumber% 10 = = 0) {

system.exit (0);

}

}

}

Java->system class

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.