System Class in Java - Properties, System.out.println() With Examples

System class in java is one of the core classes and I have never seen any java developer who doesn’t use it. One of the easiest way to log information for debugging is System.out.print() function. System class is final and all of it’s members and methods are static so that we can’t subclass and override it’s behavior through inheritance.

System class in java doesn’t provide any public constructors. So we can’t instantiate this class (for argument sake, we can instantiate it using Java Reflection) and that’s why all of it’s methods are static.

Here we will look into the different features provided by java.lang.System class.

  • Java System Array Copy

    Java System class provides a native method for copying data from one array to another. This is a native implementation and supposed to be faster than other ways to copy array data.

    System array copy method throws IndexOutOfBoundsException if copying would cause access of data outside array bounds. It also throws ArrayStoreException if an element in the source array could not be stored into the destination array because of a type mismatch and NullPointerException if either source or destination array is null.

    Below example program shows how to use this method.

     

  • Java System Properties

    System class contains useful method to get the list of System properties, get specific property, set system property and clear any existing property. The sample program below shows different methods and it’s usage.

    If we run above code in a java program, we get following output. Notice that output will vary based on your system configurations.

     

    Check out Java Properties example tutorial.

  • Reading and Writing to Console

    Java System Class provide a method to get the unique Console object associated with the running JVM.

    Console class was introduced in Java IO in 1.6 and provides useful method to print formatted data and read password securely.

    If no console is associated with the current JVM, for example running through Eclipse or running as background program, then it returns null.

    Below sample program show get Console object from System class and use it.

     

     

  • java System Get Current Time

    System class in java provide two methods to get the current time in milliseconds and nano time.

    We can use time in milliseconds to create Date object, nano time is used mostly in scientific experiments or in benchmark testing.

    Below code snippet show the usage of System class methods to get time related information.

     

  • Java System Environment Variables

    Java System class provides a method to get the environment variables data in Map form, the returned Map is unmodifiable and it contains key-value pairs in String object.

    java system environment variables, system in java

  • Security Manager

    SecurityManager class is used to implement security policy for applications, System class provide useful methods to get SecurityManager for the currently running JVM and to set the SecurityManager for the application.

     

  • File IO Operations

    System class contains three fields – in, out and err. They are used to read data from InputStream and to write data to OutputStream.

    System class provide methods to set different types of InputStream and OutputStream to be used for logging purposes.

    For example, we can set FileOutputStream to out and err fields so that console output is written to file.

    Below code snippet shows the usage of these fields and how can we set them using setter methods.

    Notice the use of Java 7 try with resources feature in above try-block.

  • Miscellaneous Tasks

    System class provides some other methods for miscellaneous tasks. For example, to run Garbage Collector, load external libraries, map the library name to OS specific String, run the finalize method for any object waiting for finalization and to terminate the JVM.

That’s all about System class in java, I hope it will help you in getting most out of java System class.

By admin

Leave a Reply