Java 简明教程

Java System Class

Introduction

Java System 类包含多个有用的类字段和方法。它不能被实例化。系统提供的便利 −

The Java System class contains several useful class fields and methods. It cannot be instantiated.Facilities provided by System −

  1. standard output

  2. error output streams

  3. standard input and access to externally defined properties and environment variables.

  4. A utility method for quickly copying a portion of an array.

  5. a means of loading files and libraries

Class Declaration

以下是 java.lang.System 类的声明 −

Following is the declaration for java.lang.System class −

public final class System
   extends Object

Field

以下是 java.lang.System 类的字段 −

Following are the fields for java.lang.System class −

  1. static PrintStream err − This is the "standard" error output stream.

  2. static InputStream in − This is the "standard" input stream.

  3. static PrintStream out − This is the "standard" output stream.

Class methods

Sr.No.

Method & Description

1

static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)This method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

2

static String clearProperty(String key)This method removes the system property indicated by the specified key.

3

static Console console()This method returns the unique Console object associated with the current Java virtual machine, if any.

4

static long currentTimeMillis()This method returns the current time in milliseconds.

5

static void exit(int status)This method terminates the currently running Java Virtual Machine.

6

static void gc()This method runs the garbage collector.

7

static Map<String,String> getenv() This method returns an unmodifiable string map view of the current system environment.

8

static String getenv(String name)This method gets the value of the specified environment variable.

9

static Properties getProperties()This method determines the current system properties.

10

static String getProperty(String key)This method gets the system property indicated by the specified key.

11

static String getProperty(String key, String def)This method gets the system property indicated by the specified key.

12

static SecurityManager getSecurityManager()This method gets the system security interface.

13

static int identityHashCode(Object x)This method returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object’s class overrides hashCode().

14

static Channel inheritedChannel() This method returns the channel inherited from the entity that created this Java virtual machine.

15

static void load(String filename)This method loads a code file with the specified filename from the local file system as a dynamic library.

16

static void loadLibrary(String libname) This method loads the system library specified by the libname argument.

17

static String mapLibraryName(String libname)This method maps a library name into a platform-specific string representing a native library.

18

static long nanoTime()This method returns the current value of the most precise available system timer, in nanoseconds.

19

static void runFinalization()This method runs the finalization methods of any objects pending finalization.

20

static void setErr(PrintStream err)This method reassigns the "standard" error output stream.

21

static void setIn(InputStream in)This method reassigns the "standard" input stream.

22

static void setOut(PrintStream out)This method reassigns the "standard" output stream.

23

static void setProperties(Properties props)This method sets the system properties to the Properties argument.

24

static String setProperty(String key, String value)This method sets the system property indicated by the specified key.

25

static void setSecurityManager(SecurityManager s) This method sets the System security.

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.lang.Object

Example: Copying an Array from a Given Source Array

以下示例展示了 Java System arraycopy() 方法的用法。在这个程序中,我们创建了两个 ints 数组并使用一些值对它们进行了初始化。现在,使用 System.arraycopy() 方法,将第一个数组 arr1 的第一个元素复制到索引为 0 的第二个数组中。然后我们打印了第二个数组以显示更新后的数组作为结果。

The following example shows the usage of Java System arraycopy() method. In this program, we’ve created two arrays of ints and initialized them with some values. Now using System.arraycopy() method, the first element of the first array arr1 is copied to second array at index 0. Then we’ve printed the second array to show the updated array as result.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      int arr1[] = { 0, 1, 2, 3, 4, 5 };
      int arr2[] = { 5, 10, 20, 30, 40, 50 };

      // copies an array from the specified source array
      System.arraycopy(arr1, 0, arr2, 0, 1);
      System.out.print("array2 = ");
      for(int i= 0; i < arr2.length; i++) {
    	  System.out.print(arr2[i] + " ");
      }
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

array2 = 0 10 20 30 40