System.out.print in java
In order to print a statement to console in Java, we have print() and println() methods available in PrintStream class. But these are instance methods, so we have to create an object to PrintStream class to invoke those instance methods.
PrintStream ps = new PrinStream();// error, There is no zero argument constructor in PrintStream, so it will fail
Approach - 1
We have to pass some output stream to create a PrintStream object.
PrintStream ps = new PrinStream(System.out);
Ps.print(“Welcome to Java”);
Approach - 2
If you refer System class there are 3 static fields in, out and err
Out – standard output stream, monitor or console
In – standard input stream - keyboard
So System.out gives us the PrintStream class object.
As we get PrintStream object by directly calling System.out, we can call print() methods available in PrintStream calss via System.out
Like System.out.println and System.out.print
No comments:
Post a Comment