Difference between System.out.println() and PrintWriter ?

Both are used to display the output. But system.out.println is used in java and out.prinln (PrintWriter) is used in scripting languages 

One thing we have to notice is, we will create the object for the class PrintWriter as "out".  out.println() is used in JSP. If we create object for that PrintWriter class as "hello", we have to print line as hello.println(). 

With System.out.println() we can print to Console. If you want to be very precise, then you have to say that System.out.println() writes to the default output stream. This is usually shown at the console, but can equally well go into a file (if you've redirected it) or nowhere (when running without a console). But System.out internally creates PrintWriter object. Not exactly. It refers to a PrintStream object. So why cant we use PrintWriter.println() directly to write to console. Because if you create a PrintWriter it is just another PrintWriter that writes to whatever you specified at the constructor. It is not the same thing as the one PrintStream that is used in System.out.

So finally there are 2 ways to write to console
1)Using System.Out.println() where System.Out is an Instance of PrintStream and by default points to Console
2)By writing in constructor of PrintWriter as mentioned above.

Disadvantage:
PrintWriter.println() is not a static method so you can just invoke the method. System.out is a PrintStream that "points to" the standard output stream and is ready for writting. To use PrintStream.println() to write to the console you would need to create a PrintStream instance that streams to standard out.


No comments:

Post a Comment

What is Normalization?

Database normalization is a data design and organization process applied to data structures based on rules that help building relational dat...