What Exactly Happens When You Call System.out.println() in Java?
This article explains the structure and behavior of System.out.println in Java, covering the System class, the out field, println method, output redirection, performance implications, comparison with logging frameworks, static import shortcuts, and the related System.err and System.in streams.
What is System.out.println()
System.out.println is a Java statement that prints its argument to the standard console output.
System class
System is a final class in the java.lang package. It provides standard input, output, error streams, access to environment variables, and utility methods.
out field
The out field is a static member of type PrintStream. It is instantiated at JVM startup and mapped to the host’s standard output console.
println method
printlnis a method of PrintStream that prints the argument followed by a line separator. The class defines many overloaded println methods, each implemented by calling print and then adding a newline; print ultimately calls write.
Structure diagram:
UML diagram
Key source snippets
public final PrintStream out
public final PrintStream err
public final InputStream in
...
public class System {
static PrintStream out;
static PrintStream err;
static InputStream in;
}
public class PrintStream extends FilterOutputStream {
public void println() { ... }
}Redirecting output
The out object can be reassigned at runtime. Using System.setOut you can redirect output to a file:
public class ChangeOut {
public static void main(String[] args) {
try {
System.setOut(new PrintStream(new FileOutputStream("log.txt")));
System.out.println("Now the output is redirected!");
} catch (Exception e) {}
}
}Performance considerations
Calling System.out.println is relatively slow because the call chain is println → print → write + newLine, and both write and newLine contain synchronized blocks. The overhead becomes noticeable when printing large amounts of data (e.g., >50 characters per line over >50,000 lines).
Why prefer a logging framework
Flexibility : Log4j provides multiple log levels, allowing selective output.
Refactorability : Changing a single configuration can disable all logging.
Maintainability : Scattered System.out.println calls are hard to manage in large codebases.
Granularity : Different classes can have separate loggers.
Practicality : Logging frameworks support output redirection, custom handlers, and richer formatting.
Therefore, System.out.println should be used only for quick experiments or learning, not for production logging or debugging.
Static import shortcut
Static import can shorten the call to out.println, but it reduces readability and is generally discouraged:
import static java.lang.System.out;
public class ShortSOP {
public static void main(String[] args) {
out.println("Hello, world");
}
}System.err and System.in
System.erris another PrintStream that writes to the standard error stream. System.in is an InputStream connected to the console keyboard. Example:
import java.io.*;
public class InOutErr {
public static void main(String[] args) {
try {
System.out.println("Enter a line");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String filename = reader.readLine();
InputStream input = new FileInputStream(filename);
System.out.println("File opened...");
} catch (IOException e) {
System.err.println("Where is that file?");
}
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
