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!
2 Answers
+ 1
@ChaoticDawg Thanks, now it makes sense.
+ 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);
}
}