[JAVA] How "System.out.println()" works?

Here your going to understand how SOP() works.  
For understanding this we need to recall few basics of Java:

  • dot (.) operator in Java: In Java . (dot operator) is used only to call methods or variables. So we can say out is either method or variable.
  • Methods in Java : we know methods always have parenthesis ‘( )’ after method name, So out cannot be a method in Java. So out its a variable and println() is a method.
  • Class name in Java: Class name should start with Capital letter ideally in Java, So System is a class.
Now with basic knowledge of Java we know :

  • System is a Class
  • out is a Variable
  • println() is a method
Lets get more in details:
out variable: static or instance?

                        Ans: static member


    The  concept in Java is all static members we can access  with Class name.
Here as I explained above System  is  predefined class and out is static member of System class which holds PrintStream object so we can call with Class name.


System.out
          
As out holds the PrintStream object we can access all  instance methods of it.


System.out.println("Hello");
        
Eg: This is how Sun mirosystem people implemented SOP();

class Print{
    void println(){
       // Print somwthing.
    }
}
 class Sys {
   static Print out1 = new Print();    
 }

public class HelloWorld{
  public static void main(String arg[]){
      Sys.out1.println();
  }
}
      




No comments:

Post a Comment