Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java run anything for System.out.println(...) when there is no console?

Does Java run any code for System.out.println(...) when I run the program in a console-less GUI? (where I don't redirect the program output to anywhere)

In fact I'd like to know if System.out.println(...) can affect the program performance when it is run by clicking the jar file.

Consider that I print thousands of lines per minute.

like image 683
Johnny Avatar asked Dec 06 '25 06:12

Johnny


2 Answers

The way Java deals with calls, there probably will be significant impact if you println something that's not a simple string.

For example,

System.out.println("Iteration " + i);

creates a StringBuilder, converts an int and copies a bunch of characters before it can even decide that there is nothing behind System.out.

Let me put it this way.

If you were to execute you program from another program, then you would be able to read the stdout of your program, so yes, there is always something sent to stdout even if nothing is "listening"

From experience, we found there was an improvement in performance (under windows), if redirected the stdout and stderr thur a null stream (which consumed the streams), but we were dealing with a lot of output not just from our program but also the RMI server we were communicating with

like image 29
MadProgrammer Avatar answered Dec 08 '25 18:12

MadProgrammer