Search code examples
javaio

System.out .write() executes but doesn't print


public class BRRead {
  public static void main(String[] args)   {
    int b;
    b='A';
    System.out .write(b);
    System.out .write('\n');
   }    
}

So when i execute the above program i get the expected output - A but when comment out the last line System.out.write('\n'); the program executes but doesn't print the output - A. Can any one explain what's exactly happening here?

public class BRRead {
  public static void main(String[] args)   {
    int b;
    b='A';
    System.out .write(b);
    //System.out .write('\n');
  }        
}

Solution

  • 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.

    In your example, flush() is not called automatically, if you call it explicitly the character will be printed.