The System class contains several useful class fields and methods. It cannot be instantiated. Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
// Returns the channel inherited from the entity that created this Java virtual machine. // This method returns the channel obtained by invoking the inheritedChannel method of the system-wide default SelectorProvider object. // In addition to the network-oriented channels described in inheritedChannel, this method may return other kinds of channels in the future. publicstatic Channel inheritedChannel()throws IOException
// Sets the System security. // If there is a security manager already installed, this method first calls the security manager's checkPermission method with a RuntimePermission("setSecurityManager") permission to ensure it's ok to replace the existing security manager. This may result in throwing a SecurityException. publicstaticvoidsetSecurityManager(final SecurityManager s);
// if a security manager has already been established for the current application, then that security manager is returned; otherwise, null is returned. publicstatic SecurityManager getSecurityManager();
// Returns the unique Console object associated with the current Java virtual machine, if any. // Returns: The system console, if any, otherwise null. // Since: 1.6 publicstatic Console console()
时间相关
这应该是大家使用最频繁的几个接口。
毫秒
1 2 3 4
// 获取当前系统时间戳(公元1970.1.1 凌晨到现在的毫秒数),native虚拟机实现 // the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. publicstaticnativelongcurrentTimeMillis();
// Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds. // This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin. publicstaticnativelongnanoTime();
// 将src数组srcPos位置开始的length元素复制到dest数组中,从dest数组中的destPos开始。 publicstaticnativevoidarraycopy(Object src, int srcPos,Object dest, int destPos, int length);
hash
返回对象的默认hashcode(即时被覆盖也不执行覆盖方法) null 返回0
1 2
// 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(). The hash code for the null reference is zero. publicstaticnativeintidentityHashCode(Object x);
// Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination. // This method calls the exit method in class Runtime. This method never returns normally. publicstaticvoidexit(int status) { Runtime.getRuntime().exit(status); }
垃圾回收
执行垃圾回收
1 2 3 4
// Runs the garbage collector. publicstaticvoidgc() { Runtime.getRuntime().gc(); }