View RSS Feed

My Java Tips

System class

Rate this Entry
by , 11-10-2011 at 07:23 PM (1293 Views)
The System class is a very useful which contains static fields and methods. It cannot be instantiated. In this post, I will briefly write about System class and will explore few interesting methods.

System class belongs to java.lang package, so it can be used in any Java class without importing any package. System class provides static methods that are very useful. For example to get current time in milliseconds:

Java Code:
static long currentTimeMillis()
This static method is very useful when you want to calculate the time taken by a piece of code. Review the code below:

Java Code:
long start = System.currentTimeMillis();
for(int i=0;i<100;i++)
long end = System.currentTimeMillis();

System.out.println("calculateSalaries executed 100 times and took "
                                             + (end - start) + " milli seconds.");
We called method 100 times and want to calculate the time it takes. We store the time in milliseconds before the execution and after the execution. Difference between the two long values gave us the execution time.

Garbage collection is called automatically but you can also call it using gc() method of System class.

Java Code:

System class has exit() method which is used to terminate the JVM.

You can load libraries (dll) and call legacy code in Java using loadLibrary(..) method.

static void loadLibrary(String libname)

System class has a method to copy array.

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


Java Code:
String array1[] = new String[5];
array1[0] = "abc";
array1[1] = "def";
array1[2] = "ghi";
array1[3] = "jkl";
array1[4] = "mno";
String array2[] = new String[3];

System.arraycopy(array1, 2, array2, 0, 3);
The attributes of current working environment are defined through properties, which are in key/value pair. The System class provides a method getProperty() which can be used to get the current working environment useful attributes. In this post, I will present examples to show how to get the working environment properties.

System properties are initialized when runtime starts. Review the following example:

Java Code:
System.out.println("User directory: " + System.getProperty("user.dir"));
System.out.println("File separator: " + System.getProperty("file.separator"));
System.out.println("Class path: " + System.getProperty("java.class.path"));
System.out.println("Class version: " + System.getProperty("java.class.version"));
System.out.println("Java home: " + System.getProperty("java.home"));
System.out.println("Java vendor: " + System.getProperty("java.vendor"));
System.out.println("Java vendor url: " + System.getProperty("java.vendor.url"));
System.out.println("Java version: " + System.getProperty("java.version"));
System.out.println("Line separator: " + System.getProperty("line.separator"));
System.out.println("OS arch: " + System.getProperty("os.arch"));
System.out.println("OS name: " + System.getProperty(""));
System.out.println("Path separator: " + System.getProperty("path.separator"));
System.out.println("User dir: " + System.getProperty("user.dir"));
System.out.println("User home: " + System.getProperty("user.home"));
System.out.println("User name: " + System.getProperty(""));

ser directory: C:Documents and SettingsjavauserworkspaceNetworking
File separator:
Class path: C:Documents and Settings javauser workspaceNetworkingbin
Class version: 49.0
Java home: C:Program FilesJavajre1.5.0_11
Java vendor: Sun Microsystems Inc.
Java vendor url:
Java version: 1.5.0_11
Line separator:

OS arch: x86
OS name: Windows XP
Path separator: ;
User dir: C:Documents and Settingsjavauser workspaceNetworking
User home: C:Documents and Settingsjavauser
User name: javauser

I displayed all the properties on the console using getProperty() method of the System class. There properties are very useful in certain scenarios for example, if you want your application to behave differently on different operation systems or on different Java versions, then the and java.version properties are of interest.

Happy coding.

Submit "System class" to Facebook Submit "System class" to Digg Submit "System class" to Submit "System class" to StumbleUpon Submit "System class" to Google