Skip to main content

How to Print Output in Java with System.out.println

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

System.out.println is how a Java program writes a line to the terminal. Its sibling System.out.print writes without ending the line, so the next output continues where it stopped.

Syntax​

Syntax
System.out.println(<value>);
System.out.print(<value>);
System.out.println();
PartWhat it does
System.outThe standard output stream.
.println(<value>)Prints the value as text, then ends the line.
.print(<value>)Prints the value without ending the line.
.println()Ends the line, which prints an empty line when nothing came before it.

The JDK 25 documentation for println: "Prints a String and then terminates the line. This method behaves as though it invokes print(String) and then println()."

The Example​

The example prints a sentence, a number, one line built from two calls, an empty line and a last line.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which prints Hello from Java and 42 with println, prints No new line, with print followed by then a new line. with println, prints an empty line with println() and then After an empty line.

Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello from Java");
System.out.println(42);
System.out.print("No new line, ");
System.out.println("then a new line.");
System.out.println();
System.out.println("After an empty line.");
}
}

Step 2: Run It​

Terminal
java Main.java
Output
Hello from Java
42
No new line, then a new line.

After an empty line.

A zsh terminal where java Main.java prints Hello from Java, 42, No new line, then a new line., an empty line and After an empty line.

  • println(42) printed the number as text.
  • print left the line open, so the next println finished it.
  • println() on its own produced the empty line.

The command is the same in zsh, bash and PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Using print for every lineEverything runs together on one lineUse println, or end with println()
Printing a null stringIt prints the word nullCheck the value first if null shouldn't appear
Printing errors with System.outThey end up with the results when output is redirectedUse System.err.println for problems

Troubleshooting​

SymptomLikely causeFix
Two outputs share a lineThe first used printSwitch it to println
null appears in the outputA variable holds nullFind where it should have been set

FAQ​

Is there a shorter way to print in Java 25?​

Yes. Java 25 added IO.println(...), which prints a line without System.out. It's handy in small programs.

Does println work with objects?​

Yes. It prints the object's toString() text, which many classes, such as LocalDate, define to be readable.

Conclusion​

System.out.println prints a value and ends the line, System.out.print leaves the line open, and System.out.println() ends a line on its own.

Additional Resources​