Professional Documents
Culture Documents
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:
Another method called System.getProperty("property_name") allows access to the following system properties:
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.