Re: Printing only an 'A' to System.out
Stefan Ram wrote:
I'll return to Java later, but first I will look at C:
The following C program has a problem:
#include <stdio.h>
int main( void ){ printf( "A" ); }
Readers might try to spot it themselves before
continuing to read the following explanation of the problem.
Spoiler:
ISO/IEC 9899:1999 (E) states:
?A text stream is an ordered sequence of characters
composed into lines, each line consisting of zero or more
characters plus a terminating new-line character. Whether
the last line requires a terminating new-line character is
implementation-defined.?
ISO/IEC 9899:1999 (E), 7.19.2#2
Thus, the last line of a text stream might require a terminating
new-line character in some implementations. The above program,
therefore, might not comply with some implementations, while
the following will never have this problem.
#include <stdio.h>
int main( void ){ printf( "A\n" ); }
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.])
I think the answer is in PrintStream.write(),
"write
public void write(int b)
Writes the specified byte to this stream. If the byte is a newline
and automatic flushing is enabled then the flush method will be invoked.
Note that the byte is written as given; to write a character that
will be translated according to the platform's default character
encoding, use the print(char) or println(char) methods."
So I think there is no guarantee that it will display without flushing
but I've never seen it happen in practice.
--
Knute Johnson
email s/nospam/knute2009/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access