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.
Typically this stream corresponds to display output or another
output destination specified by the host environment or user. By
convention, this output stream is used to display error messages
or other information that should come to the immediate attention
of a user even if the principal output stream, the value of the
variable out
, has been redirected to a file or other
destination that is typically not continuously monitored.
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data)
See the println
methods in class PrintStream
.
src
to the destination array
referenced by dest
. The number of components copied is
equal to the length
argument. The components at
positions srcPos
through
srcPos+length-1
in the source array are copied into
positions destPos
through
destPos+length-1
, respectively, of the destination
array.
If the src
and dest
arguments refer to the
same array object, then the copying is performed as if the
components at positions srcPos
through
srcPos+length-1
were first copied to a temporary
array with length
components and then the contents of
the temporary array were copied into positions
destPos
through destPos+length-1
of the
destination array.
If dest
is null
, then a
NullPointerException
is thrown.
If src
is null
, then a
NullPointerException
is thrown and the destination
array is not modified.
Otherwise, if any of the following is true, an
ArrayStoreException
is thrown and the destination is
not modified:
src
argument refers to an object that is not an
array.
dest
argument refers to an object that is not an
array.
src
argument and dest
argument refer
to arrays whose component types are different primitive types.
src
argument refers to an array with a primitive
component type and the dest
argument refers to an array
with a reference component type.
src
argument refers to an array with a reference
component type and the dest
argument refers to an array
with a primitive component type.
Otherwise, if any of the following is true, an
IndexOutOfBoundsException
is
thrown and the destination is not modified:
srcPos
argument is negative.
destPos
argument is negative.
length
argument is negative.
srcPos+length
is greater than
src.length
, the length of the source array.
destPos+length
is greater than
dest.length
, the length of the destination array.
Otherwise, if any actual component of the source array from
position srcPos
through
srcPos+length-1
cannot be converted to the component
type of the destination array by assignment conversion, an
ArrayStoreException
is thrown. In this case, let
k be the smallest nonnegative integer less than
length such that src[srcPos+
k]
cannot be converted to the component type of the destination
array; when the exception is thrown, source array components from
positions srcPos
through
srcPos+
k-1
will already have been copied to destination array positions
destPos
through
destPos+
k-1
and no other
positions of the destination array will have been modified.
(Because of the restrictions already itemized, this
paragraph effectively applies only to the situation where both
arrays have component types that are reference types.)
First, if a security manager exists, its
SecurityManager.checkPermission
method
is called with a PropertyPermission(key, "write")
permission. This may result in a SecurityException being thrown.
If no exception is thrown, the specified property is removed.
See the description of the class Date
for
a discussion of slight discrepancies that may arise between
"computer time" and coordinated universal time (UTC).
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y) consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
This method calls the exit
method in class
Runtime
. This method never returns normally.
The call System.exit(n)
is effectively equivalent to
the call:
Runtime.getRuntime().exit(n)
Calling the gc
method suggests that the Java Virtual
Machine expend effort toward recycling unused objects in order to
make the memory they currently occupy available for quick reuse.
When control returns from the method call, the Java Virtual
Machine has made a best effort to reclaim space from all discarded
objects.
The call System.gc()
is effectively equivalent to the
call:
Runtime.getRuntime().gc()
If the system does not support environment variables, an empty map is returned.
The returned map will never contain null keys or values. Attempting to query the presence of a null key or value will throw a NullPointerException . Attempting to query the presence of a key or value which is not of type String will throw a ClassCastException .
The returned map and its collection views may not obey the general contract of the Object#equals and Object#hashCode methods.
The returned map is typically case-sensitive on all platforms.
If a security manager exists, its
checkPermission
method is called with a
RuntimePermission
("getenv.*")
permission. This may result in a SecurityException
being
thrown.
When passing information to a Java subprocess, system properties are generally preferred over environment variables.
If a security manager exists, its
checkPermission
method is called with a
RuntimePermission
("getenv."+name)
permission. This may result in a SecurityException
being thrown. If no exception is thrown the value of the
variable name
is returned.
First, if there is a security manager, its
checkPropertiesAccess
method is called with no
arguments. This may result in a security exception.
The current set of system properties for use by the
method is returned as a
Properties
object. If there is no current set of
system properties, a set of system properties is first created and
initialized. This set of system properties always includes values
for the following keys:
Key | Description of Associated Value |
---|---|
java.version |
Java Runtime Environment version |
java.vendor |
Java Runtime Environment vendor | java.vendor.url |
Java vendor URL |
java.home |
Java installation directory |
java.vm.specification.version |
Java Virtual Machine specification version |
java.vm.specification.vendor |
Java Virtual Machine specification vendor |
java.vm.specification.name |
Java Virtual Machine specification name |
java.vm.version |
Java Virtual Machine implementation version |
java.vm.vendor |
Java Virtual Machine implementation vendor |
java.vm.name |
Java Virtual Machine implementation name |
java.specification.version |
Java Runtime Environment specification version |
java.specification.vendor |
Java Runtime Environment specification vendor |
java.specification.name |
Java Runtime Environment specification name |
java.class.version |
Java class format version number |
java.class.path |
Java class path |
java.library.path |
List of paths to search when loading libraries |
java.io.tmpdir |
Default temp file path |
java.compiler |
Name of JIT compiler to use |
java.ext.dirs |
Path of extension directory or directories |
os.name |
Operating system name |
os.arch |
Operating system architecture |
os.version |
Operating system version |
file.separator |
File separator ("/" on UNIX) |
path.separator |
Path separator (":" on UNIX) |
line.separator |
Line separator ("\n" on UNIX) |
user.name |
User's account name |
user.home |
User's home directory |
user.dir |
User's current working directory |
Multiple paths in a system property value are separated by the path separator character of the platform.
Note that even if the security manager does not permit the
getProperties
operation, it may choose to permit the
operation.
First, if there is a security manager, its
checkPropertyAccess
method is called with the key as
its argument. This may result in a SecurityException.
If there is no current set of system properties, a set of system
properties is first created and initialized in the same manner as
for the getProperties
method.
First, if there is a security manager, its
checkPropertyAccess
method is called with the
key
as its argument.
If there is no current set of system properties, a set of system
properties is first created and initialized in the same manner as
for the getProperties
method.
java.util.Hashtable
.
The general contract of hashCode
is:
hashCode
method on each of
the two objects must produce the same integer result.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
This method returns the channel obtained by invoking the inheritedChannel method of the system-wide default java.nio.channels.spi.SelectorProvider object.
In addition to the network-oriented channels described in inheritedChannel , this method may return other kinds of channels in the future.
The call System.load(name)
is effectively equivalent
to the call:
Runtime.getRuntime().load(name)
libname
argument. The manner in which a library name is mapped to the
actual system library is system dependent.
The call System.loadLibrary(name)
is effectively
equivalent to the call
Runtime.getRuntime().loadLibrary(name)
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 time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.
For example, to measure how long some code takes to execute:
long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime;
wait
methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
synchronized
statement
that synchronizes on the object.
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
wait
methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
Calling this method suggests that the Java Virtual Machine expend
effort toward running the finalize
methods of objects
that have been found to be discarded but whose finalize
methods have not yet been run. When control returns from the
method call, the Java Virtual Machine has made a best effort to
complete all outstanding finalizations.
The call System.runFinalization()
is effectively
equivalent to the call:
Runtime.getRuntime().runFinalization()
If there is a security manager,
its checkExit
method is first called
with 0 as its argument to ensure the exit is allowed.
This could result in a SecurityException.
First, if there is a security manager, its checkPermission
method is called with a RuntimePermission("setIO")
permission
to see if it's ok to reassign the "standard" error output stream.
First, if there is a security manager, its checkPermission
method is called with a RuntimePermission("setIO")
permission
to see if it's ok to reassign the "standard" input stream.
First, if there is a security manager, its checkPermission
method is called with a RuntimePermission("setIO")
permission
to see if it's ok to reassign the "standard" output stream.
Properties
argument.
First, if there is a security manager, its
checkPropertiesAccess
method is called with no
arguments. This may result in a security exception.
The argument becomes the current set of system properties for use
by the
method. If the argument is
null
, then the current set of system properties is
forgotten.
First, if a security manager exists, its
SecurityManager.checkPermission
method
is called with a PropertyPermission(key, "write")
permission. This may result in a SecurityException being thrown.
If no exception is thrown, the specified property is set to the given
value.
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
.
Otherwise, the argument is established as the current
security manager. If the argument is null
and no
security manager has been established, then no action is taken and
the method simply returns.
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.The current thread must own this object's monitor.
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition }(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.