Internal working of System.out.println in Java
ID:1 | Views:0 | Likes: | Update date:NOV 17, 2024 01:19 AM

I remember when I was attending Java class first time, everyone crying “Yes Sir” when sir was asking, Guys, Do you have potential to lean Java?
I got little bit surprise that why he was asking for potential, actually he wanted to say, Learners need to have guts, patience and strong practice to build the Java Developer. Later on I understand it and will explain you in details in Java Introduction Blog. As of now I can say Java is not tough but need your honest efforts to own it.

It is special day for me when I am writing first blog for you and remembering those moments when I was beginner like you. The thing which I leaned first time was “How to print data on Java console”. I believe that you will learn and enjoy simultaneously with me.

To print the data on Java console, you need to pass data as an argument into println() method. The process of printing the data on console is not same as Java implementation is different from other languages.
Let’s have a look of printing the data on console

  • System is a final class found in java.lang package that contains Native API and provides the facilities to interact with hardware or standard devices like monitor, keyboard.
  • out is an pre-defined object of PrintStream class and instantiated during startup and get mapped with video buffer memory. It is declared as public, static and final variable in System class.
  • println()method of PrintStream class takes an argument to print to standard console. It is overloaded and calls print() and newline() methods.

Step-by-Step Data Flow Explanation

  • Data to be printed is passed into print() or println() method. You can pass any type of data as these methods are overloaded.
  • out object is pre-defined of PrintStream class. It is initilized at startup time and already mapped with video buffer memory but can be customized. It invokes to print() or println()method
  • Data is bind into out object and ready to transfer to System
  • Now we need class or methods which helps us to interact with the hardware i.e. Monitor
  • Java.lang.System is an class that contains native API also and helps us to interact with the hardware
  • Out object is transferred to System class and declared as public, static and final variable in System class
  • Now out passed into setOut() method of System and check IO devices by IOCheck() method.
  • After that the native method setOut0(out) is called and finally data transferred to monitor buffer memory
  • When data is stored into video buffer memory then it the monitor’s responsibility begins to display the data on screen

The object out can be Customized

Out object belongs to PrintStream class and gets initialized by JVM at startup and it’s behavior can be changed by developer during execution. By default it is used to print output on command prompt or Java console but developer can change its behavior by passing argument into setOut() method. Instead of passing ‘out’ object, you can create new PrintStream object with new file name and the output redirect to text file in current directory not in console.

All three static stream objects, ‘out’, ‘err’, ‘in’ can be customized by following methods.

  • System.setOut()
  • System.setErr()
  • System.setIn()

Lets build a example to record the time of multiple execution of System.out.println() and compare with multiple execution of write() method with 512-bytes buffer and specify the ASCII as character encoding to be used.

Performance Evaluation

In below example the statement 'System.out.println()' executes 10,000 times and you can see the execution time of printing the data.

Output : Execution Time : 184 ms

In below example the satement 'out.write()' executes 10000 times and you can see the execution time of printing the data.

Output : Execution Time : 20 ms

Can we short it ?</i>

System.out.println() is one of most repeatable statement in Java and looks long in typing. We can little bit short it with static import but I am not recommend you because of its poor readability.
In eclipse you can write syso and then press CTRL + Space
Static import is a Java feature introduced in JDK 1.5 that allow you to access the static members directly without class or object.

System.out is a PrintStream object and writes output on Java console. It is already mapped with video buffer but developer can change its behavior. This is generally used from command line tools like notepad.

System.err is a PrintStream object and writes output on Java console like System.out but generally it is used to print error message on console. i.e. In Eclipse it will show the output in RED color.

System.in is an InputStream object and used to take input from standard devices like keyboard as it is already mapped with input buffer. It is specifically used for console and not used in GUI.

Guys, Hope you like this blog, Please write your feedback or suggestions in comment box. Your appreciation or suggestion are valuable for us and encourage us to write more.

Tarique Anwer
NOV 17, 2024 01:19 AM
Report Error

Article helpful ?

shantilal
01/11/11
Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi iusto ut vero! Repellat id quos numquam ullam sit! Vitae tempora ullam repudiandae consequuntur perspiciatis a libero magni nam, sapiente quisquam!
Reply

Switch

Java
Java
Python
Python
Php
Php
Blogs

How did Java become a Platform Independent language ?

Why Multi-threading application's performance always appreciated?

Let's Understand the handling process of String and StringBuffer

What is Reference value and Why Java does not support Pointers?

Why local variable is no longer accessible out of the methods ?

Home

Training

Enquiry

Assessment

More