How the data prints on Java console ?


Brief Introduction

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 the console is not the same as Java implementation is different from other languages. 
Let’s have a look of printing the data on the console.

For more clear and explanation click here
  • 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 a pre-defined object of PrintStream class and instantiated during startup and get mapped with video buffer memory. It is declared as a public, static and final variable in System class.
  • println()method of PrintStream class takes an argument to print to the standard console. It is overloaded and calls print() and newline() methods.



public final class System {
 public final static PrintStream out = null;
}
public class PrintStream extends FilterOutputStream implements Appendable, Closeable
{
 public void println(int x){
 synchronized (this){
  print(x);
  newLine();
 }
 }
}

Step-by-Step Data Flow Explanation

Before go with step-by-step, Let’s first try to understand with a real-time example.
Let’s assume a class equals to a family. So when you or a family member, means an object goes to another family (relatives) then you have to introduce yourself. By mistake, if you entered in an unknown family and introduced yourself even still they would ask you who are you? And finally will not allow you to proceed. In a similar way, an object has to be declared in the class if it is coming from another class.



So now, we can easily understand the steps for the same:

  • 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 initialized 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 a class that contains native API also and helps us to interact with the hardware
  • Out object is transferred to System class and declared as a 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 the screen

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

Comments