• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is System.out a PrintStream and not a PrintWriter?

 
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:
I am trying to understand the difference between the PrintStream and PrintWriter.
I see that they have identical methods.

for instance,

void println(float x) Prints a floating-point number and then terminates the line.



Since System.out takes characters, for example when writing to the console, shouldn't it have been a PrintWriter instead of being a PrintStream?
 
Saloon Keeper
Posts: 15608
366
  • Number of slices to send:
    Optional 'thank-you' note:
System.out has been part of Java since version 1.0. PrintWriter was "only" added in version 1.1.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Stephan van Hulst wrote:System.out has been part of Java since version 1.0. PrintWriter was "only" added in version 1.1.


Thanks you
 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
Also, System.out and System.in were at that time connected to operating-system entities which consisted of streams of bytes. I believe they still are for most operating systems where Java runs, in fact.
 
Enthuware Software Support
Posts: 4828
52
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:I am trying to understand the difference between the PrintStream and PrintWriter.
I see that they have identical methods.


They don't have identical methods. Specifically, methods of PrintStream do not throw IOException. PrintStream is more of a utility class (a hack) that is used primarily for System.in/out/err and has methods to print everything. PrintWriter, introduced later, otoh, is proper "Writer". You will notice other differences too in the JavaDoc.
 
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Anilprem wrote:They don't have identical methods. Specifically, methods of PrintStream do not throw IOException.


Neither do methods of PrintWriter.  The constructors of both PrintStream and PrintWriter throw IOException, but the methods do not.  

What's weird about both classes, though, is that while they extend OutputStream and Writer, you usually have no need to use most of the methods of those classes - except close(), maybe a few others.  But if you're using either PrintStream or PrintWriter, you want to use the various print() methods, not the various write() methods.  Note that those write() methods are where PrintStream and PrintWriter are actually different - but there's no good reason to use them.  I would say this is a design flaw.

Originally, the most important difference between PrintStream and PrintWriter was in the constructors.  A PrintStream could connect to OutputStreams, and a PrintWriter could connect to a Writer.  So you would choose one or the other depending on what you were connecting to.  However, over time, this difference diminished, as they added additional constructors to both classes to make it easier to connect either to whatever underlying stream/writer/file you want to.  Note that you can connect a PrintWriter to an OutputStream, but you can't connect a PrintStream to a Writer.  So far.

At this point, PrintWriter does everything you might want, and I would just use that wherever you can - except for System.out, which is still a PrintStream.  But usually, that difference will not matter.
 
Paul Anilprem
Enthuware Software Support
Posts: 4828
52
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:
Neither do methods of PrintWriter.  The constructors of both PrintStream and PrintWriter throw IOException, but the methods do not.  


You are right. I must have confused it with some other class.

Mike Simmons wrote:
Note that you can connect a PrintWriter to an OutputStream, but you can't connect a PrintStream to a Writer.  So far.


A PrintWriter is, after all, a Writer, while PrintStream is not. A Writer adds encoding logic on top of a raw byte stream, so connecting a PrintStream to a Writer would be like putting the cart before the horse.
 
Saloon Keeper
Posts: 27851
196
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
It just occurred to me — and Paul touches on that — that a PrintWriter (as he says), has extra support logic and is sensitive to the data being printed. If memory serves, that includes keeping a buffer which is flushed when a newline is output, either directly (via println()) or as part of the data stream.

But System.out is directly affiliated with the Unix stdout and there are many, many programs which write non-text, that is, pure binary data to stdout. For example, a lot of video, audio, and image processing utilities. You never know when a JPEG being output might have a "0x0a" (newline) in the binary data, resulting in weird action.

So that's also an incentive not to use PrintWriter for System.out.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
Both PrintStream and PrintWriter support auto-flush of the buffer, as an optional feature controlled by a boolean parameter at construction time.  On my MacBook Pro using OpenJDK 21, the System.out I see in the debugger comes with auto-flush enabled.  But I don't think that's a documented requirement - other systems may give you a System.out with auto-flush disabled.

After testing code in a debugger and investigating the source code, it looks like PrintStream is a lot more aggressive about auto-flush than PrintWriter is.  Calling just about any method on a PrintStream with auto-flush on seems to invoke flush() at (or very near) the end of the method call.  But for a PrintWriter with auto-flush on, it only auto-flushes when you call one of the println() methods.  Calling print() or write() doesn't auto-flush at all.  I suspect they decided they had gone a bit overboard with the initial auto-flush implementations in PrintStream, and used a milder version for PrintWriter - but were reluctant to change it in PrintStream, for fear of altering behavior of existing code.

Anyway, neither PrintStream nor PrintWriter seem to be changing data based on whether it looks like a newline character.   For PrintStream some methods do check for newline to decide whether to auto-flush or not. When a newline appears as part of an array of data, they simply pass it on unmolested, regardless of whether it's a "\n" or a "\r\n" or whatever - they just pass along what they are given.   But if you call one of the println() methods, those methods append a system-dependent newline to the end of whatever else they print.

So, I don't think the auto-flush or newline behavior would have any effect on a program's ability to write binary data.   What does have an effect, however, is the the character encoding used by PrintWriter.  Also the very API of a Writer is problematic, since it's all in char[] rather than byte[].  So yeah, Java programs still need some sort of OutputStream to write binary data to standard out.  
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |