What is System and out in System.out.println() in Java

A basic question pop up in mind while doing programs and when facing interviews
What is

   System and Out in System.out.println()

in JAVA..??
exactly How it works internally ..??
In this post we are going to do postmortem of this System.out.println();

Objective :

1.       What Is System in System.out.println();
2.       What is .out in SOP…?
3.       If .println(), .print() is method in PrintStream Class then why we are calling it from System      Class.
4.        Print  Hello on console and in  “test.txt” File java Program : .....You 'll Enjoy this Program
5.        Summary of SOP .


1.What Is System in System.out.println();
System is a class made available by Java to let us manipulate various operating system related objects. It's from java.lang package .

System is final class it means we cannot Inherit class System .
Can we create Object of it…………….??
No cz java has also make its default constructor as private  ( Very Smart .…


See in Image above so we cannot create inherit it and cannot create Object out of it.
As per javadoc, “…>System  class provide services to
  standard input, standard output, and error output streams;”
  we can say it as utility class.

2.What Is out in System.out.println();
Out is a static variable inside  System class is variable  declared as object of PrintStream class.
Out variable  actually pointing to PrintStream Class to use  functionalities provided by PrintStream Class

                                                              Something pop up in your mind…?Why we need out variable to use functionality                                                                                                                                                                          provided by PrintStream Class

Can we :
1.     Create object of PrintStream class in our code and write op : YES
Following program ‘ll clear your all Doubts..

Program to print Hello on console and in file "test.java".

import java.io.*;

 public final  class TP {
public static void main(String a[]){

try{
PrintStream f = new PrintStream(
     new FileOutputStream("test.txt"));
System.out.println("Folowing p.println(hello ) is in file  test.txt \n");

f.println("hello");      // fis printstream Obj calling println() method

PrintStream p = System.out;    // p pointing to System class PrintStream Object 

p.println("Hello i am here on console  by PrintStream Object p  \n");
System.out.println(" I am on console your Old Friend  System.out.println()  ");

}
catch(Exception e){}

  }
}


3. If .println(), .print() is method in PrintStream Class then why we are calling it from System Class.
Answer lies in Above program , We use System class to call println() method because we want to put output on console in case if we want  to put output on file or any other than Console then we are not going to use out variable in System class.


4. Print  Hello on console and in  “test.txt” File java Program :
Please see program mention in this post.


5.Summary of System.out.println();

  • System is final class in java it is use to manipulate various operating system related objects in our case we are using it to print output on console
  • ‘out’ is a static member variable in System Class whose return type is PrintStream { here System class Actually pointing to PrintStream Class using out variable.
  • Println()/print() is method in PrintStream class out variable is accessing this methods From System class.  








No comments:

Post a Comment