You are on page 1of 2

The System class holds a collection of static methods and variables.

This class handles all the activities regarding the standard input and output. The standard input, output, and error output of the java runtime are store in the in, out, err variables. Some of the methods defined in the system class are as follows. void CurrentTimeMillis() return the current time in terms of milliseconds since midnight, January 1,1970 void gc() - Initiate the garbage collector. void exit(int code) halts the execution and returns the value of integer to parent process usually to an operating system.

import java.io.IOException; public class Elapsed { public static void main(String[] args) throws IOException { long lngStart, lngEnd = 0; System.out.println("Timing a for from 0 to 1,000,000"); //time a for loop from 0 to 1,000,000 lngStart=System.currentTimeMillis(); for(int j=0;j<1000000;j++) lngEnd = System.currentTimeMillis(); System.out.println("Elapsed time : " + (lngEnd-lngStart)); } }
Output:

Timing a for from 0 to 1,000,000 Elapsed time: 78

The System Class


The System class provides access to the native operating system's environment through the use of static methods. As an example System.currentTimeMillis() retrieves the system clock setting (as a long, in milliseconds counted from January 1, 1970). Some of the available methods are: currentTime() exit(int freeMemory() status) getenv(String var)

gc() exec(String totalMemory() cmd) execin(String cmd)

getCWD() getOSName() arraycopy(src[], srcpos, dest[], destpos, len)

Another method called System.getProperty("property_name") allows access to the following system properties:

file.separator line.separator path.separator os.arch os.name os.version

user.dir user.home user.name java.class.path java.class.version java.home

java.vendor java.vendor.url java.version java.vm.name java.vm.vendor java.vm.version

Note: Many System class methods can throw a SecurityException exception error for safety reasons. The System class also provides standard io redirectable print streams for console read, write and error operations. System.in and System.out refer to the user console by default but can be redirected by the operating system to files using the symbols < and >. System.err always refers to the console (no redirection allowed). The method read() returns an integer value. It can also throw an IOException exception error. The methods print(string), println(string) and printf(format, object_list) display strings [with line return] [using c-like syntax formatting] to the console. There are much better ways for user interactions. Refer to file io for basic file management or to file choosers for visual file interfaces using Swing objects.

You might also like