0

The Javadoc for PrintStream#print(char) states

Prints a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

So this means that following code should print 2 'a' however prints one 'a' not two.

System.out.print('a');
System.out.write('a');

Can some one help me understand this behaviour

Lovis
  • 9,513
  • 5
  • 31
  • 47
T-Bag
  • 10,916
  • 3
  • 54
  • 118
  • This will print two `a`s. Are you saying that in your case it is printing only one `a`? – VHS Mar 31 '17 at 04:52
  • 1
    Try flushing... – shmosel Mar 31 '17 at 04:52
  • @VHS Yes..it is printing 1 a .. :( – T-Bag Mar 31 '17 at 04:53
  • I don't understand the negative vote here. The question is well formed and within the scope of SO – Mangat Rai Modi Mar 31 '17 at 05:24
  • @4castle, your link points to PrintWriter class, not PrinttStream. [Here](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/io/PrintStream.java#PrintStream.print%28char%29) is the correct link. If you see this code, you will see that there is a different implementation of print from that of write. It is a design decision. – VHS Mar 31 '17 at 05:25
  • @MangatRaiModi, I didn't downvote it. But I think someone downvoted it because the OP mentioned in a comment that there is some contradiction in the documentation of these two methods and yet he didn't clarify what contradiction. – VHS Mar 31 '17 at 05:30

1 Answers1

3

As per the java docs of PrintStream#write

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.

So just call flush.

Call System.out.flush(); after System.out.write('a');.

Alternately,

As the docs suggest, set the output stream to autoflushable and then write a new line char at the end of your program. In fact the PrintStream object System.out is already set to autoflushable if you look at the source code System class. So really, all you need to do is just print a new line character in the end. No need to call flush.

System.out.print('a');
System.out.write('a');
System.out.write('\n');
VHS
  • 9,534
  • 3
  • 19
  • 43
  • Yes but if i go by the docs of print() char then statement is contradicting. – T-Bag Mar 31 '17 at 05:05
  • 1
    @ShowStopper There is no 'link of `char()`' in your question. – user207421 Mar 31 '17 at 05:08
  • 1
    Oh you meant print(char). I had already checked that link. it doesn't state that it requires automatic flushing set to true. – VHS Mar 31 '17 at 05:08
  • @ShowStopper Nor is `'a'` a newline. There is no contradiction. Your point remains obscure. – user207421 Mar 31 '17 at 05:09
  • Thanks @EJP. ShowStopper, if you have reservations against using flush, you can set the output stream to autoflush and then just print a new line in the end. Let me edit my answer to show it. – VHS Mar 31 '17 at 05:13
  • Why does the `print(char)` flush, but `write(int)` doesn't? – 4castle Mar 31 '17 at 05:18
  • 1
    @4castle, I would say that's by class design. The java docs clearly say flush is required for write. But they don't say that for print. – VHS Mar 31 '17 at 05:22