class System

class System

class System Introduction

The java.lang.System class comes with many static methods and mainly intended to communicate with the underlying operating systems. Using these methods, it is possible to take keyboard input (using, knowing system time (using currentTimeMillis()), copying an array elements into another (using arraycopy()), to advise to go for garbage collection (using gc()) and finally to know the system properties etc.

Following is the class signature

public final class System extends Object

The System class is declared as final and thereby no class is allowed to inherit. The class contains three static final instance variables.

public static final InputStream in
public static final InputStream out
public static final PrintStream err

The in variable represents an object of and out and err variables represent an object of and their usage is discussed more in I/O streams.

Following program illustrates the usage of currentTimeMillis() and getProperties() methods.

class System
Output screen of class System

long startLoop = System.currentTimeMillis();

currentTimeMillis() static method returns the system time in milliseconds (this method is also used to know the file copying time in I/O topic). It should be noted that JDK1.5 adds one more method nanoTime() method that returns the time in nano seconds. In the above statement, the time is read before while loop iterations starts. Similarly, the time is taken after while loop and the difference gives you the time taken for iterations.

Properties props = System.getProperties();

The getProperties() static method of System class returns an object of java.util.Properties object. Properties is a data structure that stores the data in key/value pairs. The method returns the properties like Java vendor and path separator etc.

Enumeration e1 = props.propertyNames();

The propertyNames() method of Properties returns Enumeration object. Using Enumeration object we can extract all the property names and their corresponding values in a loop. For example, the property name is and its value printed is US.

arraycopy() method is illustrated in arrays topic.

Map map1 = System.getenv();
Set set1 = map1.keySet();

The static method getenv() returns the path and classpath etc. as an object of java.util.Map interface. To get the values, a Set object is obtained using keySet() method. Set and Iterator interfaces are discussed in Data Structures.

Another property linked with System class, used very often, is System.out.pritnln().

Leave a Reply

Your email address will not be published. Required fields are marked *