What is Java’s system.out.println and how does it work?

What is Java's system.out.println and how does it work?

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

The question on what is Java system.out.println and how it works is common among amateur programmers as well as a few experienced ones. Many interviewers won?t expect you to know the answer but would like to see how you think and arrive at an answer to a related question. This information provides you with the description of each word in the statement and what it does behind the scenes.

What is system.out.println?

? System is a class built into the Java language, and it is specified within the Java.lang package. This class features an ultimate modifier, implying that another class can’t use it.

? Out is a variable in the static final field in System class of the type PrintStream. The PrintStream is an inbuilt class that contains methods to print the various data values. Static fields and methods must be accessed through the class name which is the System.out. Therefore, the expression System.out refers to an object of the type PrintStream.

? printIn is a method of PrintStream class used to print the data values. It receives an expression as an argument and presents it in String form to the standard output window. There are several printIn overloaded methods with distinct arguments. Each printIn makes a call to print method and adds a newline. Therefore to retrieve a method in PrintStream class, we use out.print().

In a nutshell, System.out.println is an output function used in Java where System denotes the name of the package, out denotes the class name, and print is a function in that class. Thus, the expression shows texts to the command window.

How does the system.out.println expression work?

When using this expression, we do not have to use a System object to print messages to the screen. We only call the println method of the public staticPrintStream member on the System?s class, out. When you want to print the standard output, you will use System.out. Alternatively, you can pass System.out to PrintStream and then invoke println on PrintStream object to print the output.

Let?s put aside the theory and illustrate what we mean.

PrintStream ps = new PrintStream (System.out) ;
Ps.printIn (?Hello People!?) ;
Ps.println (?Hello World!?) ;
Ps.flush () ;

D: \JavaPrograms>javac SystemOutPrintlnDemo.java
D: \JavaPrograms>javac SystemOutPrintlnDemo
Hello People!
Hello World!


We hope that this explanation and illustration has given you a clear understanding of what Java system.out.println is and how it works. Please let us know how this article was helpful in the comment section below.

Recent Posts