Exploring Java Class `^} System

System Class JavaDoc

Let’s explore System class as provided in java.lang package, part of jdk.

SystemDeclar

  1. System class is final, so we cannot extend it.
  2. Java compiler will not accept the following statement:

    SystemExtends

  3. Constructor of System class is private, so we cannot instantiate it.
  4. Java compiler will not accept the following statement:

    ObjectSystem

  5. Standard Input is treated as an object and is represented as a field inside System class. Suppose you have your keyboard and you want to save what you type into some variables (files/objects as an extension) in your program, you can use System.in
  6. InDeclar

    In below code in reads a byte from standard input and assigns to int. Note that character as a byte is read from standard input and assigned to int.

    In

    While experimenting with above code, you might want to check out ASCII table to match the output of the above code.

  7. Standard Output is treated as an object and is represented as a field inside System class. Suppose you have your monitor, you want to show something on the screen, you can use System.out
  8. OutDeclar

    In below code out prints an integer onto standard output.

    Out

  9. System Properties

COMMON SCENARIOS WHERE SYSTEM CLASS IS USED

We might bump into following scenarios at various points in time while solving problems.
We can leverage System class to solve these problems.

[
A caveat before trying to access System properties. If the runtime does not have security access to read the properties, then following code will get null in place of the property.
]

  1. Let’s write code to find country of the user
  2. GetUserCountry_Normal

    Let’s rewrite the above code using a wrapper utility created by Apache for System class.
    You can download Apache Commons Lang package here

    GetUserCountry

  3. Let’s write code to find name of the user. This is username of the user logged in, under which the java program is currently running
  4. As we become more lazy, we might start leveraging SystemUtils provided by Apache Commons Lang for any work to do with printing out System properties. Note that Apache’s SystemUtils does not provide an API to modify System’s properties.(Modification of System’s properties can be done using mutators provided by System class, limited by SecurityManager) It provides API to retrieve data about System properties in easier manner.

    GetUserName

  5. Let’s write code to find current working directory. This is similar to running ‘pwd’ command in linux, which is ‘print working directory’
  6. GetUserDir

  7. Let’s write code to find language of the user.
  8. Java uses language codes which are defined by a standards body.

    GetUserLang

  9. Let’s write code to find operating system of the user.

Here, I dont want to use Apache’s SystemUtils, since it provides a lot of boolean constants which need to be branched using if-else-if, instead I would just use plain System.getProperty() and get the OS name.

GetOSName

There is much more to be explored in System class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s