Header Ads

All About System.out.println Method In Java

java
From the day one while learning Java we use to give this command to print some value to console. It may be for output purpose or for debugging purpose.
Now let us understand what is System,what is out and what is println?
System is a built in class which is present in java.lang package. This class is of final modifier type so it can not be inherited to other class. or it can not be instantiated. This package defines a class called System.The System class holds a collection of static methods and variables.the standard input,output and error output of the java runtime are stored in in,out,err variables.
out is a static final field (ie, variable)in System class which is of the type PrintStream (a built-in class, contains methods to print the different data values).
static fields and methods must be accessed by using the class name, so ( System.out ).  In a simple way ..out is a static member of type PrinStream class(java.io) declared in the System class.static members of a class can be accessed directly by classname that's why System.out.out here denotes the reference variable of the type PrintStream class. As this is of type static ,
  1. It gets instantiated as soon as the class is loaded.
  2. It creates a mapping with the console where the program is running
  3. The stream becomes open and ready for accepting data.
println() is a public method in PrintStream class to print the data values. Hence to access a method in PrintStream class, we use out.println() (as non static methods and fields can only be accessed by using the reference variable)
Note- There are multiple println() method exists with different parameter 
The code snippet might help you to understand better.. 

public final class java.lang.System {    
public static final java.io.PrintStream out;     
// some methods . . .     
}     
public class java.io.PrintStream extends.......{     
public void println(....){     
. .     
}     
} 



More details to find on java.lang.system class check out here..
So this command can not be altered and package can not be modified. We can not create our own variable to do print.
like
system.XYZ.print(“Test”);
is invalid
==============================================
But surely you can represent this method in different other ways…
One of the way may be--


import java.io.*;

public class animesh     
{     
private static PrintStream out = System.out;     
public static void main(String args[])     
{     
out.println("This is good");     
}     
}    
 


Here instead of System.out.println() we are using out.println()
==============================================
Another way may be--
import javax.swing.JOptionPane;    
import java.io.*;     
public class animesh {    

 public static void main(String[] args) {     
String str=JOptionPane.showInputDialog("enter data");     
System.out.println(str);     
animesh d=new animesh();     
d.print(str);     
}     
public void print(Object obj){     
System.out.println(obj.toString());     
}     
}     


Here I have modified some other way….here instead of System.out.println() I am using d.print(str).
These are all wrapper technique on top of System.out.println()  Nothing else!!
Let me know if you enjoyed this post.
Powered by Blogger.