gstackoverflow
gstackoverflow

Reputation: 37002

Why does PrintStream inherit from byte streams?

As far as I know java.io ist divided into byte streams and character streams. Byte streams for reading and writing bytes and character streams for reading and writing characters.

PrintStream has following inheritance hierarchy:

Object <- OutputStream <- FilterOutputStream <- PrintStream.

Thus I made resolution that PrintStream related with reading and writing bytes.

But if write something like this:

    PrintStream output = new PrintStream(System.out);

    output.print(true);
    output.print((int) 123);
    output.print((float) 123.456);

    output.printf(Locale.UK, "Text + data: %1$d", 123);

    ...

I see characters in my console. This situation confused me. Please clarify my misunderstanding. What do the terms byte stream and character stream really mean?

Upvotes: 2

Views: 254

Answers (1)

Stephen C
Stephen C

Reputation: 719661

The PrintStream class is a semantic anomaly that exists for historical reasons. The class existed in Java 1.0 before the Java designers recognized1 the need to have different "stacks" for character and byte streams. It represents "the original way" to do simple text output in Java ... before Writer and its subtypes were introduced in Java 1.1.

So in fact, byte stream and character stream (probably) mean exactly what they seem to you to mean. At any rate, what they mean is best described in the Java Tutorial:


1 - They probably realised the need earlier, but there were overriding "commercial reasons" to get Java 1.0 released quickly.

Upvotes: 2

Related Questions