Printing only an 'A' to System.out

S

Stefan Ram

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.])
 
B

Bojan

you can use System.out.println which will automatically add "\n" to
the end of your line.

The program should work otherwise. although i have never seen "final"
String[] args
 
L

Lew

Bojan said:
The program should work otherwise.
although i [sic] have never seen "final" String[] args

You've never seen that before, although 'final' method arguments are
not all that uncommon an idiom, but you know 'final' is a Java
keyword, and you know what it does. Ergo, you know in what way it
would influence the behavior of the method.
 
B

Bojan

The program should work otherwise. although i have never seen "final"
String[] args

It is part of a coding style commandment, which goes like: "ye shall
not modify a method argument as if it were a mere local variable". And
its companion commandment: "ye shall punish thyself with the help of
the compiler and the 'final' keyword, should ye dare to ignore the
previous commandment".

I am fully aware of what final stands for. I have never seen it used
for the parameter for the main method.
 
A

Arved Sandstrom

Bojan said:
you can use System.out.println which will automatically add "\n" to
the end of your line.
[ SNIP ]

That's not the point - Stefan knows this. He's asking what happens _if_
the last line does not end with a newline.

AHS
 
K

Knute Johnson

Stefan said:
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.
 
K

Karl Uppiano

Knute Johnson said:
Stefan said:
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.

Would this be one of the places that Java simply cannot "abstract away"
platform-specific behavior?
 
K

Knute Johnson

Karl said:
Knute Johnson said:
Stefan said:
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.

Would this be one of the places that Java simply cannot "abstract away"
platform-specific behavior?

That's probably a good way to put it. STDOUT on a Windows system will
print a single character without a linefeed or a flush. So if that is
home it's probably fine. We used to use cprintf, the console printf, to
do things like that with C under OS/2 in the old days, just for that reason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top