Thursday, December 30, 2004

Basics of System class

According to the specifications of Java Programming Language, the facilities provided by this class are :

a) Standard input, output and error streams
b) Access to the defined "properties"
c) means of loading files and libraries and
d) utility method for quickly copying a portion of an array.

The most common use we do in our programs is to put debug statements on console, using:

System.out.println(....).

Being the static class, the call to an object/method can be made as System.methodName or System.objectName and not by making the object of this class. In System.out.println: "out" is an object of PrintStream and println is the method of that class. One of the important feature to note here is that the "out" and "err" objects are of PrintStream which is the sub-class of OutputStream. But the "in" is the object of the InputStream. The advantages of PrintStream object will be done sometime later.

The next important feature but least commonly used is to determine the System properties. There is a native method called initProperties(). This method guarantees that the following properties are defined always:

KeyDescription of Associated Value
java.versionJava Runtime Environment version
java.vendorJava Runtime Environment vendor
java.vendor.urlJava vendor URL
java.homeJava installation directory
java.vm.specification.versionJava Virtual Machine specification version
java.vm.specification.vendorJava Virtual Machine specification vendor
java.vm.specification.nameJava Virtual Machine specification name
java.vm.versionJava Virtual Machine implementation version
java.vm.vendorJava Virtual Machine implementation vendor
java.vm.nameJava Virtual Machine implementation name
java.specification.versionJava Runtime Environment specification version
java.specification.vendorJava Runtime Environment specification vendor
java.specification.nameJava Runtime Environment specification name
java.class.versionJava class format version number
java.class.pathJava class path
java.library.pathList of paths to search when loading libraries
java.io.tmpdirDefault temp file path
java.compilerName of JIT compiler to use
java.ext.dirsPath of extension directory or directories
os.nameOperating system name
os.archOperating system architecture
os.versionOperating system version
file.separatorFile separator ("/" on UNIX)
path.separatorPath separator (":" on UNIX)
line.separatorLine separator ("\n" on UNIX)
user.nameUser's account name
user.homeUser's home directory
user.dirUser's current working directory


Multiple paths in a system property value are separated by the path separator character of the platform.

So, System.getProperty("line.separator") will return the line separator based on which OS ..etc etc.

This class is also used to load the files and libraries, using methods load(fileNameWithFullPathAsString) and loadLibrary(libNameAsString). The standard strategy is to call like:

static { System.loadLibrary("LibFile");

This gets loaded as the class is intialized. These methods can be also called as:

Runtime.getRuntime().loadLibrary(name)

Finally, there is a utility method to copy an array beginnin from the specified array
position to a specified position. The signature of the methods is as:

public static void arraycopy(Object src, int srcPos, Object dest,
int destPos, int length)

The src and dest object here can be same.

Finally, the method exit(int i): a nonzero status code indicates abnormal termination.

0 Comments:

Post a Comment

<< Home