Re: Printing only an 'A' to System.out
Stefan Ram wrote:
[... is a '\n' required at the end of every text output line? ...]
Now, my question regarding Java: Is anything wrong with
the following Java program?
public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.print( "A" ); }}
The output might look ugly, because it might not be separated
from text following it properly, but is there a wording
similar to ISO/IEC 9899:1999 (E), 7.19.2#2 somewhere in a Java
specification that might condemn this program in a similar
manner to ISO/IEC 9899:1999 (E), 7.19.2#2?
(7.19.2 is in C because the I/O facilities offered by the
environment (operating system) might require a terminating
linefeed. If a Java implementation is running in the same
environment, it should inherit the same limitations from the
same environment. So, if Java guarantees that the last line
never requires a terminating new-line character, how does it
do that? [One answer would be: by excluding those environments
that require a terminating new-line character.])
Perhaps someone can find a more definitive answer, but it seems
to me that the question comes down to the behavior of the flush()
method of whatever OutputStream subclass ultimately underlies the
PrintStream. The Javadoc for OutputStream#flush() says
If the intended destination of this stream is an abstraction
provided by the underlying operating system, for example a
file, then flushing the stream guarantees only that bytes
previously written to the stream are passed to the operating
system for writing; it does not guarantee that they are
actually written to a physical device such as a disk drive.
From this, I'd say that if the platform requires a trailing '\n' and
you don't write one, you're on shaky ground. Perhaps the platform
will synthesize a '\n' for you, or perhaps it will just lose the final
line of output. And the behavior may vary depending on the nature of
the destination: Files, pipes, sockets, and printers might all behave
idiosyncratically in the absence of the '\n'.
Maybe a less roundabout (or even contradictory) answer exists
somewhere, but I haven't found it.
--
Eric.Sosman@sun.com