Java System.out.println()
In this page:
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
public class Main {
public static void main(String[] args) {
System.out.println(42);
System.out.println("moves to the next line automatically");
}
}
Login to try C/C++/Java/PHP code in the editor
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()
public class Main {
public static void main(String[] args) {
System.out.print("Same ");
System.out.print("line"); // continues right where the last print ended
}
}
Login to try C/C++/Java/PHP code in the editor
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
public class Main {
public static void main(String[] args) {
int score = 90;
System.out.println("Score: " + score); // non-string joined into one string
}
}
Login to try C/C++/Java/PHP code in the editor
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
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3); // prints 5, not the text "2 + 3"
}
}
Login to try C/C++/Java/PHP code in the editor
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
public class Main {
public static void main(String[] args) {
System.out.println("Line one\nLine two\tTabbed");
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: