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 System.in), 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 java.io.InputStream and out and err variables represent an object of java.io.PrintStream and their usage is discussed more in I/O streams.

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

import java.util.*;
public class TimeAndProperties
{
  public static void main(String args[])
  {
                               	          // time for executing the while loop
    long startLoop = System.currentTimeMillis();          
    int x = 1;
    while(x <= 10000000)
    { x++; }
    long endLoop = System.currentTimeMillis();      
    System.out.println("Starting time of loop in milliseconds: " + startLoop);
    System.out.println("Ending time of loop in milliseconds: " + endLoop);
    System.out.println("while loop took " + (endLoop - startLoop) + " milliseconds for " + x + " iterations");

                                 // to get the OS properties
    System.out.println("\n\t\tSystem properties are:\n");  
    Properties props = System.getProperties();
    Enumeration e1 = props.propertyNames();
    while(e1.hasMoreElements())
    {
      String k1 = (String) e1.nextElement();
      String v1 = props.getProperty(k1);
      System.out.println(k1 + " : " + v1);
    }
			  // to get environment variables
    System.out.println("\n\t\tFollowing are system environment variables:\n");  
    Map map1 = System.getenv();            
    Set set1 = map1.keySet();
    Iterator it = set1.iterator();
    while(it.hasNext())
    {
      String key = (String) it.next();
      String value = (String) map1.get(key);
      System.out.println(key + " : " + value);
    }
  }
}


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 user.country 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 Comment

Your email address will not be published.