Java provides us with a comprehensive set of pre-built classes and libraries which reduces the need of overhead coding. One such class is System class in Java. In this article, I will be talking about various concepts that constitute this class and how they make it one of the most widely used among Java Developers.
Below are the topics I will be discussing in this article:
- System Class in Java
- java.lang.System Class Declaration
- Class Fields
- System Class Methods
- Implementing System class methods
Let’s get started.
System Class in Java
The System is one of the core classes in Java and belongs to the java.lang package. The System class is a final class and do not provide any public constructors. Because of this all of the members and methods contained in this class are static in nature. Thus you cannot inherit this class to override its methods. Since the System class in Java comes with so many restrictions, there are various pre-built class fields and methods available. Below I have listed down a few of the important features supported by this class:
- Standard input and output
- Error output streams
- Access to externally defined properties and environment variables
- Built-in utility for copying a part of an array
- Provides means for loading files and libraries
Now that you are aware of what exactly is System class in Java, let’s move ahead and find out, how to declare this class.
java.lang.System Class Declaration
Below I have demonstrated the declaration for java.lang.System class:
public final class System extends Object
The System class in Java comes with various inbuilt class fields and methods. Let us now move further in this article and learn about them one by one, starting with the class fields.
Class Fields
The java.lang.System class comes with three fields which are:
- public static final InputStream in: This is the standard input stream in Java programming. This stream is already open and available for supplying the input data. This input stream mainly corresponds to the keyboard inputs or other input sources that are specified by the host environment or a user.
- public static final PrintStream out: This is the standard output stream in Java programming. This stream is already open and available for accepting the output data. This output stream mainly corresponds to displaying the output or another output destination that is specified by the host environment or a user.
- public static final PrintStream err: This is the standard error output stream in Java programming. This stream is already open and available for accepting the output data. This output stream mainly corresponds to displaying the output or another output destination that is specified by the host environment or a user. Technically, this output stream is used for displaying the error messages or other information that needs the immediate attention of a user.
Now that you are aware of the class fields of System class in Java, let’s now take a look at the various methods provided by this class.
System Class Methods
There is a total of 28 in-built methods declared in the java.lang.System class. Below I have listed down each of them along with their explanations.
Let’s now try to implement some of these methods of System class in Java in the next section of this article.
Implementing System class in Java
In the following example, I have implemented a few of the above-discussed methods.
package edureka; import java.io.Console; import java.lang.*; import java.util.*; public class SystemClassMethods { public static void main(String[] args) { String a[]= {"D","P","R","E","K","A"}; //source array String b[]= {"E","D","U","V","O","I","D","L","E","A","R","N","I","N","G"}; //destination array String src[],dest[]; int srcPos,destPos,length; src=a; srcPos=2; dest=b; destPos=3; length=4; System.out.print("Source array:"); for(int i=0;i<src.length;i++) {System.out.print(a[i]);} System.out.println(); System.out.print("Destination array:"); for(int i=0;i<dest.length;i++) {System.out.print(b[i]);} System.out.println(); System.out.println("Source Position:"+srcPos); System.out.println("Destination Position:"+destPos); System.out.println("Length:"+length); System.arraycopy(src, srcPos, dest, destPos, length); //use of arraycopy() method System.out.println("After Copying Destination Array: "); for(int i=0;i<b.length;i++) { System.out.print(b[i]); } System.out.println(); System.out.println("---------Implementing NanoTime Method----------"); System.out.println("Current time in nanoseconds = "+System.nanoTime()); System.out.println(); System.out.println("---------Implementing getProperties() Method----------"); System.out.println("Your System property for user"); Properties p = System.getProperties(); System.out.println(p.getProperty("user.name")); //property to get User's account name System.out.println(p.getProperty("user.home")); //property to get User's home directory System.out.println(p.getProperty("user.dir")); //property to get User's current working directory System.out.println(); System.out.println("---------Implementing console() Method----------"); Console console = System.console(); if(console != null){ Calendar c = new GregorianCalendar(); console.printf("Welcome %1$s%n", "Edureka"); console.printf("Current time is: %1$tm %1$te,%1$tY%n", c); console.flush(); } else{ //No console is attached when executed in Eclipse System.out.println("No Console attached"); } System.out.println(); System.out.println("---------Implementing getSecurityManager() Method----------"); SecurityManager secManager = System.getSecurityManager(); if(secManager == null){ System.out.println("SecurityManager is not configured"); } SecurityManager mySecManager = new SecurityManager(); System.setSecurityManager(mySecManager); secManager = System.getSecurityManager(); if(secManager != null){ System.out.println("SecurityManager is now configured"); } } }
Output
Source array:DPREKA Destination array:EDUVOIDLEARNING Source Position:2 Destination Position:3 Length:4 After Copying Destination Array: EDUREKALEARNING ---------Implementing NanoTime Method---------- Current time in nanoseconds = 433367948321300 ---------Implementing getProperties() Method---------- Your System property for user Swatee_Chand C:UsersSwatee_Chand C:UsersSwatee_Chandeclipse-workspaceSystemClass ---------Implementing console() Method---------- No Console attached ---------Implementing getSecurityManager() Method---------- SecurityManager is not configured SecurityManager is now configured
You can try to implement rest of the methods and in case you get stuck somewhere, you can drop a comment and we will help you with it.
With this, we come to the end of this article on System class in Java. If you want to know more about Java you can refer to our other Java Blogs.
Now that you have understood what is System class in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA Training and Certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “System class in Java” article and we will get back to you as soon as possible.