Java System Class

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The facilities provided by the System class of java consist of standard input and output along with error and output stream to access defined properties externally as well as the environment variables. It also provides a utility method for copying a portion of an array quickly and a means for loading files and libraries.

Introduction to Java System Class

The java system class consists of several useful functions for class fields and methods. It also provides facilities like the standard input and output as well as the error output stream. The java system class can’t be instantiated. The class system of java resides in the module of java.base and the package of java.lang. The java system class has 3 different kinds of fields along with 28 different kinds of methods.

Declaration

The class declaration of the java.lang.System class is as follows:

Nested Class Summary of Java System Class

Modifier and class of nested classClassDescription
static interfaceSystem.LoggerThe System.Logger is used for instances of log messages that would be routed to the logging framework LoggerFinder
static classSystem.LoggerFinderThe service LoggerFinder is used for creating, managing as well as configuring the logger to its underlying framework and its uses.

Field Summary of Java System Class

Modifier and type of fieldFieldDescription
static PrintStreamerrIt is the standard error output stream
static InputStreaminIt is the standard input stream
static PrintStreamoutIt is the standard output stream

Fields in Detail

in

This is the standard input stream and is already open and ready for supplying the input data. This stream typically corresponds to the keyboard input or another input source for that matter which is specified by the user or the host environment.

out

This is the standard output stream which is already open and is ready for accepting the output data. This stream typically corresponds to displaying the output or any other output destination as specified by the user or the host environment.

The simple way of writing output data is as follows:

err

This is the standard error output stream that is already open and is ready to accept the output data.

This stream typically corresponds to displaying the output or any other output destination as specified by the user or the host environment. This output stream, by convention, is used for displaying the error message or any other important information that requires immediate action from the user.

Methods Summary of Java System Class

Method nameDescription of the method
arraycopy(object src, int srcPos, object dest, int destPos, int length)This method is used for copying the components of the specified source array to that of the specified destination array.
clearProperty(String key)This method is used for removing the property of the system which is indicated by the specified key.
console()This method is used for returning the object of the console which is associated with the JVM of java also known as the java virtual machine.
currentTimeMillis()This method is used for returning the current time which is in the format of milliseconds. This returned time will be a unit of time.
exit(int status)This method is used for terminating the current java virtual machine which is running on the system. This method, as an augment, takes a status code.
gc()This method is used for running the garbage collector
getenv()This method is used for returning the map view of the string of the current system environment where the string map is unmodifiable and the environment is dependent on the system.
getLogger(String name, RecourseBundle bundleThis method is used for getting the localized instance of the logger and its instance can be used for the caller’s use
getLogger(String name)This method is used for returning the instance of the logger and its instance can be used for the caller’s use
getenv(String name)This method is used for returning the value of an environment variable which is specified and is dependent on the system's external named value
getProperties()This method is used for returning the properties of the current system, where the properties are the properties that the JVM gets from the operating system.
getProperty(String key)This method is used for returning the property of the system which is indicated by the specified key as mentioned in the argument.
getProperty(String key, String def)This method is used for returning the property of the system which is indicated by the key specified.
getSecurityManager()This method is used for returning the interface of the system’s security
identityHashCode(Object x)This method is used for returning the hash code for the object specified and is returned by the default hashCode() method.
inheritedChannel() throws IOExceptionThis method is used for returning the channel that is inherited from an entity that has created the JVM also known as Java virtual machine
lineSeparator()This method is used for returning the line separator string which is dependent on the system and returns the same value every time.
load(String filename)This method is used for loading the file which is specified by the argument filename where the name of the argument must be an absolute name of the path
mapLibraryName(String libname)This method is used for mapping the name of the library into the platform-specific string that represents the native library
nanoTime()This method is used for returning the high-resolution time source in nanoseconds by running the java virtual machine and then returns the current value of the JVM
runFinalizersOnExit(boolean value)This method is used for running the finalization methods that can be of any object
runFinalization()This method runs finalization methods which can be of any object pending finalization.
setErr(PrintStream err)This method is used for reassigning the standard error output stream
setIn(PrintStream in)This method is used for reassigning the standard input stream
setOut(PrintStream out)This method is used for reassigning the standard output stream
setSecurityManager(SecurityManager s)This method is used for setting the security of the system
setProperties(Properties props)This method is used for setting the properties of the system to the properties of an argument
setProperty(String key, String value )This method is used for setting the property of a system that is specified by the key

Methods in Detail

1. setIn

It reassigns the standard input stream. The method checkPermission is executed which calls the RuntimePermission("setIO") method to check if it is ok to reassign the input stream.

Parameters:

  • in - the new standard input stream.

Throws:

  • SecurityException:
    It is thrown when the method checkPermission does not allow reassigning of the input stream.

2. setOut

It reassigns the standard output stream. The method checkPermission is executed which calls the RuntimePermission("setIO") method to check if it is ok to reassign the input stream.

Parameters:

  • out - the new standard output stream

Throws:

  • SecurityException:
    It is thrown when the method checkPermission does not allow reassigning of the input stream.

3. setErr

It reassigns the standard output stream which is standard. The method checkPermission is executed which calls the RuntimePermission("setIO") method to check if it is ok to reassign the input stream.

Parameters:

  • err - the new standard error output stream.

Throws:

  • SecurityException:
    It is thrown when the method checkPermission does not allow reassigning of the input stream.

4. console

It is used for returning the object console with is associated with the current java virtual machine.

Returns:

  • If there is any system console then it returns it, else it returns null

5. inheritedChannel

It is used for returning the channel that is inherited from that entity that created the JVM, also known as java virtual machine. The returned channel is obtained by this method by invoking the method inheritedChannel of the system.

Returns:

  • If there are any inherited Channel then it returns it, else it returns null

Throws:

  • IOException:
    If an I/O error occurs
  • SecurityException:
    It is thrown when access is not given

6. setSecurityManager

It is used for setting the system-wide security manager. If in case the security manager is already installed then it first calls the method checkPermission along with permission RuntimePermission("setSecurityManager") to ensure that it is ok to replace the security manager. Else the argument is established as the current security manager but if the argument is null then it returns the method and no action is taken.

Parameters:

  • sm - the security manager or null

Throws:

  • SecurityException:
    It is thrown when it doesn’t allow to replace
  • UnsupportedOperationException:
    it is thrown when the sm is not null and the security manager is not set dynamically.

7. getSecurityManager

The security-wide system manager is accessed by this method

Returns:

  • It returns the security manager if it is already established for the current application else it returns null

8. currentTimeMillis

It is used for returning the current time in milliseconds. For instance, any operating system that you might use returns the time in units of ten milliseconds.

Returns:

  • the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

9. nanoTime

It is used for returning the current value of the java virtual machine which is running with a high resolution of time source around nanoseconds. This method is only used for measuring the elapsed time and not any other system. The precision of this nanosecond method is accurate to some extent.

Returns:

  • It is used for returning the current value of the java virtual machine which is running with a high resolution of time source around nanoseconds

10. arraycopy

It is used for copying the array from the beginning position where the source array is specified to the destination position which is too specified in the argument of an array. Here the source is referenced as src whereas the destination is referenced as dest. The number of elements that can be copied from the source to the destination array is equal to the length of an array.

The NullPointerException is thrown if dest and src are null. The ArrayStoreException is thrown if the following condition is true:

  • If the src present in the argument points to an object that is different from the array.
  • If the dest present in the argument points to an object that is different from the array.
  • If the arguments src and dest in an array point to an array whose type of component is different from type primitive.
  • The argument src points to an array of type of component primitive and argument dest points to an array with type component of reference.
  • The argument src points to an array of type of component reference and argument dest points to an array with type component of primitive.

The IndexOutOfBoundsException is thrown if any of the following values is true:

  • The argument srcPos is negative
  • The argument destPos is negative
  • The argument length is negative
  • When the srcPos+length is greater than the length of the source array, src.length
  • When the destPos+length is greater than the length of the destination array, dest.length

Parameters:

  • src - it is a reference to the source array.
  • srcPos - it is a reference to the starting position in the source array.
  • dest - it is a reference to the destination array.
  • destPos - is a reference to the starting position in the destination data.
  • length - it is a reference to the number of array elements to be copied.

Throws:

  • IndexOutOfBoundsException
    If copying of data would result in data outside the bound of an array.
  • ArrayStoreException:
    If an element in the array src is different in the type of that present in the dest array.
  • NullPointerException:
    If either src or dest is null.

11. identityHashCode

For the given object in the argument, it returns the hash code of the same. For the null reference, the hash code is zero.

Parameters:

  • x - it is an object for which the hashCode is to be calculated

Returns:

  • the hashCode

12. identityHashCode

For the given object it returns the same hash code as returned by the default hash code method.

Parameters:

  • x - it is an object for which the hashCode is to be calculated

Returns:

  • the hashCode

13. getProperties

It is used for determining the properties of the current system. If a security manager exists, then the method checkPropertiesAccess is called with no arguments. If no set of current system properties exists then it is first created and initialized.

Returns:

  • The current system properties

Throws:

  • SecurityException :
    If a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of the system.

14. lineSeparator

It returns the line separator string that is dependent on the system. It always returns the same value which is the initial value of the line.separator system property.

It returns "\n" On UNIX systems, and it returns "\r\n" on Microsoft Windows systems.

Returns:

  • This method returns the system-dependent line separator string

15. setProperties

It is used for setting the properties of the system to the argument Properties.

Parameters:

  • props - is the new system properties.

Throws:

  • if a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of the system.

16. getProperty

It is used for getting the property of the system as indicated by the specified key.

Parameters:

  • key - it is the name of the property of the system.

Returns:

  • It returns the value of the string of the system property, if there is no property with that key then in that case it returns null.

Throws:

  • SecurityException:
    If a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of a system.
  • NullPointerException:
    This is thrown when the key is null.
  • IllegalArgumentException:
    It is thrown when the key is empty.

17. getProperty

  • It gets the property of the system which is specified by that key in an argument.

Parameters:

  • key - it is the name of the property of the system.
  • def - it is a default value.

Returns:

  • It returns the string value of the system property which is specified, or it returns the default value if there is no property with that key specified.

Throws:

  • SecurityException:
    If a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of the system.
  • NullPointerException:
    This is thrown when the key is null.
  • IllegalArgumentException:
    It is thrown when the key is empty.

18. setProperty

  • It is used for setting the property of the system as specified in an argument.

Parameters:

  • key - it returns the name of the property specified.
  • value - it returns the value of the property specified.

Returns:

  • It returns the previous value of the system property, if there is no value then it returns null.

Throws:

  • SecurityException:
    If a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of a system.
  • NullPointerException:
    This is thrown when the key is null.
  • IllegalArgumentException:
    It is thrown when the key is empty.

19. clearProperty

  • It is used for removing the properties of the system as specified by the key in an argument.

Parameters:

  • key - it is the name of the system property that is to be removed as specified in an argument.

Returns:

  • It returns the previous value of the string of the system property, and if there is no property associated with that key then it returns null.

Throws:

  • SecurityException:
    If a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of a system.
  • NullPointerException:
    This is thrown when the key is null.
  • IllegalArgumentException:
    It is thrown when the key is empty.

20. getenv

Parameters:

  • name - it returns the name of the environment variable specified in the argument.

Returns:

  • It returns the string value of the variable that is specified, if in case the variable is not defined in the environment of the system then it returns null.

Throws:

  • SecurityException:
    If a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of a system.
  • NullPointerException:
    This is thrown when the key is null.
  • IllegalArgumentException:
    It is thrown when the key is empty.

21. getenv

It returns the string view of the map which is can not be modified in the current environment of the system. It is dependent on mapping the system names to a value which in the process is passed from parent to child. In case the environment is not supported by the system then an empty map is returned.

Returns:

  • It returns the environment as a map of variable names to values

Throws:

  • SecurityException:
    If a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of a system.

22. getLogger

It is used for returning the instance of logger which is in user for caller

Parameters:

  • name - it is the name of the logger that is specified by the developer in an argument.

Returns:

  • It returns an instance of System.Logger that can be used for calling the class.

Throws:

  • NullPointerException:
    It is thrown if the variable name is null.
  • IllegalCallerException:
    It is thrown if there is no Java caller frame on the stack.

23. getLogger

It is used for returning the localizable instance of the logger which is used by the caller. The logger that is returned will be used for providing the bundle of resouthe rce for the localization of the message.

Parameters:

  • name - it is used for returning the name of the logger.
  • bundle - it returns a bundle of resources.

Returns:

  • It returns an instance of the System.Logger which will use for providing the bundle of resources for message localization.

Throws:

  • NullPointerException:
    It is thrown if the variable name of the bundle is null.
  • IllegalCallerException:
    It is thrown if there is no Java caller frame on the stack.

24. exit

It is used when we have to terminate a running java virtual machine. The argument that it takes is a status code and if it is a non-zero then the code indicates an abnormal termination.

Parameters:

  • status - it is the exit status.

Throws:

  • SecurityException:
    If a security manager exists and its checkPropertiesAccess method doesn't allow access to the properties of a system.

gc

It is used for running the garbage collector that calls the gc method from JVM, a java virtual machine, and recycles the unused object to free up the space of memory for reuse.

The method called System.gc() is equivalent to the method call: Runtime.getRuntime().gc().

25. runFinalization

It is used for running the method finalization used for discarding the objects of the method by using a java virtual machine. It is called by using the method System.runFinalization().

26. load

It is used for loading the library that is specified in the argument. The filename in the argument is the absolute pathname of the library. It is called by using System.load(name).

27. mapLibraryName

It is used for mapping the name of the library into a string that is platform specific and that represents the library.

Parameters:

  • libname - is the name of the library.

Returns:

  • It is used for returning a platform-dependent native library name.

Throws:

  • NullPointerException:
    It is thrown when the name of libname is null

Java System Class Examples

Example - 1

Write a program to copy the array from source to destination

Output:

Explanation:

In this example, the method arraycopy() is used for copying the elements from the source array to the destination array. In the argument, we have also specified how we want the length or how many elements are to be copied.

Example - 2

Write a program to get the current time in Java.

Output:

Explanation:

In this example, the method currentTimeMillis() is used for getting the current time of the system in milli seconds as seen from the output.

Example - 3

Write a program to use the offline separator method.

Output:

Explanation:

In this example, the line separator method is used for separating the two values and putting the second value on the next line as seen from the output.

Implementing Java System Class Methods

Write a program in java to implement system classes like src and length

Output:

Explanation:

Here, all the system class methods are implemented. You can see the output to understand which method is used in which cases. Note that some methods might produce different outputs on different local machines.

Conclusion

  • The facilities provided by the System class of java consist of standard input and output along with error and output stream to access defined properties externally as well as the environment variables.

  • It also provides a utility method for copying a portion of an array quickly and a means for loading files and libraries.

  • The java system class consists of several useful functions for class fields and methods. It also provides facilities like the standard input and output as well as the error output stream. The java system class can’t be instantiated.

  • The class system of java resides in the module of java.base and the package of java.lang. The java system class has 3 different kinds of fields along with 28 different kinds of methods.

  • Declaration:

    The class declaration of java.lang.The system class is as follows: