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

Java System.out.println()

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
public class Main {
	public static void main(String[] args) {
		System.out.println(42);
		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
public class Main {
	public static void main(String[] args) {
		System.out.println("Line one\nLine two\tTabbed");
	}
}
🔒

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.