Why there is no outputs ! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why there is no outputs !

I've been recently learning about I/O in java. However I tried a code about "write()" method defined by "PrintStream" Class, here it's: public class Program { public static void main(String[] args) { int b; b = 's'; System.out.write(b); System.out.write('\n'); } } //outputs: s The problem occurs when I delete the last line(System.out.write('\n');), there is simply no outputs. Where did 's' go!

21st Jul 2017, 4:51 AM
omar alhelo
omar alhelo - avatar
2 Answers
+ 1
@ChaoticDawg Thanks, now it makes sense.
21st Jul 2017, 5:24 AM
omar alhelo
omar alhelo - avatar
+ 1
Because you are using the write() method. The write method doesn't flush the stream automatically unless/until there is a newline character found. You can call the flush method directly to get the output. public class Program { public static void main(String[] args) { int b; b = 's'; System.out.write(b); System.out.flush(); } } https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#write(int) https://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#flush() You should really be using the print or println methods to output to the console though. public class Program { public static void main(String[] args) { int b; b = 's'; System.out.println((char)b); } }
21st Jul 2017, 5:26 AM
ChaoticDawg
ChaoticDawg - avatar