Print a boolean value with printf

I assume you are running into a buffering issue, such that your program exits before your buffer flushes. When you use printf() or print() it doesn’t necessarily flush without a newline. You can use an explicit flush()

boolean car = true;
System.out.printf("%b",car);
System.out.flush();

or add a new-line (which will also cause a flush())

boolean car = true;
System.out.printf("%b%n",car);

See also Buffered Streams – The Java TutorialsFlushing Buffered Streams which says in part

Some buffered output classes support autoflush, specified by an optional constructor argument. When autoflush is enabled, certain key events cause the buffer to be flushed. For example, an autoflush PrintWriter object flushes the buffer on every invocation of println or format.

Leave a Comment