← Back to Core Java Course | Chapter 2: Input & Output | Lesson 1 of 6

Java System.out.println()

System.out.println is how a Java program shows messages on your screen, like a loudspeaker announcing something. println also moves to a new line afterward.
Syntax
java
System.out.println(value);
System.out.print(value);

Basic Printing

println() converts whatever you pass it (numbers, strings, objects) into its text form and writes it to the console, then moves the output cursor to the start of the next line automatically.

Example: Basic Printing

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(42);
		// Print a line to the console
		System.out.println("moves to the next line automatically");
	}
}

print() vs println()

print() leaves the cursor right where the output ended, so a second print() call continues on the same line -- useful for building up one line of output across multiple statements, unlike println().

Example: print() vs println()

java
public class Main {
	public static void main(String[] args) {
		System.out.print("Same ");
		System.out.print("line"); // continues right where the last print ended
	}
}

Concatenating Output

Using the + operator inside println(), like println("Score: " + score), converts non-string values to text and joins them into a single string before printing -- Java evaluates left to right.

Example: Concatenating Output

java
public class Main {
	public static void main(String[] args) {
		int score = 90;
		System.out.println("Score: " + score); // non-string joined into one string
	}
}

Printing Expressions

Java computes any arithmetic inside the parentheses first -- println(2 + 3) prints 5, not the literal text "2 + 3" -- so mixing math and string concatenation in one line requires care with operator precedence.

Example: Printing Expressions

java
public class Main {
	public static void main(String[] args) {
		System.out.println(2 + 3); // prints 5, not the text "2 + 3"
	}
}

Special Escape Characters

Escape sequences like \n (newline) and \t (tab) let you control spacing and line breaks inside a single string literal without needing multiple print statements.

Example: Special Escape Characters

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("Line one\nLine two\tTabbed");
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using print() when you want a new line, so the next output appears on the same line.
  2. Forgetting to escape quotes inside a string, as in "He said "hi"", which is a compile error; use \".
  3. Writing System.out.println("a" + 1 + 2) and expecting arithmetic, when the numbers are joined as text.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.