![]() ![]() ![]() ![]() |
Using System Resources |
Copying Arrays
Use System'sarraycopy()
method to copy data from one array into another. Thearraycopy()
method requires five arguments:The two Object arguments indicate the array to copy from and the array to copy to. The three integer arguments indicate the starting location in each the source and the destination array, and the number of elements to copy. This diagram illustrates how the copy takes place:public static void arraycopy(Object source_array, int src_location, Object destination_array, int dest_location, int length,[PENDING: picture]
The following program uses
arraycopy()
to copy some elements from thecopyFrom
array to thecopyTo
array.Theclass ArrayCopyTest { public static void main(String args[]) { byte copyFrom[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' }; byte copyTo[] = new byte[5]; System.arraycopy(copyFrom, 6, copyTo, 0, 5); System.out.println(new String(copyTo, 0)); } }arraycopy()
method call in this example program begins the copy at element number 6 in the source array--remember that array indices start at 0 so that the copy begins at the array element 'w'. Thearraycopy()
method call puts the copied elements into the destination array beginning at the first element (element 0) in the destination arraycopyTo
. The copy copies 5 elements: 'w', 'o', 'r', 'l', and 'd'. Effectively, thearraycopy()
method copies the "world" part ofcopyFrom
intocopyTo
, like this:[PENDING: picture]
Note that the destination array must be allocated before you call
arraycopy()
andGetting the Current Time
ThecurrentTimeMillis()
method returns the current time in milliseconds since 00:00:00 UTC, January 1, 1970. ThecurrentTimeMillis()
method is commonly used during performance tests: get the current time, perform the operation that you want to time, get the current time again--the difference in the two time samples is "roughly" the amount of time that the operation took to perform.Often in graphical user interfaces the time difference between mouse clicks is used to determine whether a user double clicked in the window. The following applet uses
currentTimeMillis()
to compute the number of milliseconds between two mouse clicks. If the time period between the clicks is smaller than 200 milliseconds, the two mouse clicks are interpreted as a double mouse click.
Here's the source for the TimingIsEverything applet shown above.
You could use the return value from this method to compute the current date and time. However, you'll probably find that it's more convenient to get the current date and time from the Date
class.
Note: Previous versions of the System class support two other time-related methods besides thecurrentTimeMillis()
method:currentTime()
andnowMillis()
. These two methods are now obsolete--you should usecurrentTimeMillis()
instead.currentTime()
andnowMillis()
are no longer supported.
Exiting the Runtime Environment
To exit the Java interpreter, call theSystem.exit()
method. You pass an integer exit code to theexit()
method, and the interpreter exits with that exit code.
Note: Theexit()
method causes the Java interpreter to exit, not just your Java program. You should use this function with caution, particularly in applets.Setting and Getting the Security Manager
The security manager is an object that enforces a certain security policy for a Java application. You can change the current security manager for your applications using System'ssetSecurityManager()
method, and you can retrieve the current security manager using System'sgetSecurityManager()
method.Providing Your Own Security Manager
discusses security managers in detail and shows you how to create and install your own security manager using these two System methods.
Note: Applets cannot change the security manager. [PENDING: can they "get" it?] The security manager for applets is created and managed by the application in which the applet is running.
See also
java.lang.SecurityManager
![]() ![]() ![]() ![]() |
Using System Resources |