6 Replies Latest reply: Jul 9, 2008 8:24 AM by 843785 RSS

    difference between System.out.println(..)and PrintWriter.println(..)

    843785
      Hello every one

      With System.out.println(..) we can print to Console.

      But System.out internally creates PrintWriter object.So why cant we use
      PrintWriter.println(..) directly to write to console.

      Is there aany disadvvantage with PrintWriter.println(..).

      If so can anyone please tell me what is that and

      what is the advantage we r going to get with System.out.println(..) method of writing to console.
        • 1. Re: difference between System.out.println(..)and PrintWriter.println(..)
          843785
          If you are using the same OutputStream in PrintWriter as System.out, then there is no real difference, apart from the fact that you're simply recreating what already exists, like reinventing the wheel.

          Vincent
          • 2. Re: difference between System.out.println(..)and PrintWriter.println(..)
            843785
            kirn291 wrote:
            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.
            what is the advantage we r going to get with System.out.println(..) method of writing to console.
            The advantage is that it's the only way to do it. Could you post an example of how you'd think of a different way to write to the console?
            • 3. Re: difference between System.out.println(..)and PrintWriter.println(..)
              843785
              System.out is an instance of PrintWriter, like you say. But it's already been created, and directing the stream to stdout. So what advantage would there be for you to do it yourself? None, really. But you could do, if you wanted to
              • 4. Re: difference between System.out.println(..)and PrintWriter.println(..)
                800323
                kirn291 wrote:
                Hello every one

                With System.out.println(..) we can print to Console.

                But System.out internally creates PrintWriter object.So why cant we use
                PrintWriter.println(..) directly to write to console.
                Actually, System.out is a PrintStream not a PrintWriter.

                >
                Is there aany disadvvantage with PrintWriter.println(..).

                If so can anyone please tell me what is that and

                what is the advantage we r going to get with System.out.println(..) method of writing to console.
                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.
                • 5. Re: difference between System.out.println(..)and PrintWriter.println(..)
                  843785
                  hello every one

                  so what u say is we should create an object of type PrintStream like

                  PrintStream ps=new PrintStream();

                  And with in the consrtuctor of PrintStream we should write code to print output to console.

                  can anyone give me a small hint of how to do it?

                  So finally there are 2 ways to write to console

                  1)Using System.Out.println() where System.Out is an Instanse of PrintStream and by default points to Console

                  2)Second one is as said above
                  • 6. Re: difference between System.out.println(..)and PrintWriter.println(..)
                    843785
                    kirn291 wrote:
                    hello every one

                    so what u say is we should create an object of type PrintStream like

                    PrintStream ps=new PrintStream();

                    And with in the consrtuctor of PrintStream we should write code to print output to console.

                    can anyone give me a small hint of how to do it?

                    So finally there are 2 ways to write to console

                    1)Using System.Out.println() where System.Out is an Instanse of PrintStream and by default points to Console

                    2)Second one is as said above
                    No. Nobody is saying that. We're all saying, don't bother creating your own stream to stdout, since one already exists for you