System.out.println() prints a line of text to the console and moves the cursor to the next line. This is how you see output from your programs — and in automation, how you debug test failures.
public class PrintDemo {
public static void main(String[] args) {
System.out.println("Login test started");
System.out.println("Entering username");
System.out.println("Entering password");
System.out.println("Clicking submit");
System.out.println("Test passed");
}
}Login test started
Entering username
Entering password
Clicking submit
Test passedprintln adds a newline after printing. print does not — the next output continues on the same line. printf lets you format output with placeholders.
public class PrintTypes {
public static void main(String[] args) {
// println — adds newline
System.out.println("First line");
System.out.println("Second line");
// print — no newline
System.out.print("Same ");
System.out.print("line ");
System.out.println("ends here");
// printf — formatted output
String testName = "LoginTest";
int passed = 8;
int total = 10;
System.out.printf("Test: %s | Passed: %d/%d%n", testName, passed, total);
}
}First line
Second line
Same line ends here
Test: LoginTest | Passed: 8/10| Format | Meaning | Example |
|---|---|---|
| %s | String | "Hello" |
| %d | Integer | 42 |
| %f | Decimal | 3.140000 |
| %.2f | Decimal (2 places) | 3.14 |
| %n | Newline | (platform-safe) |
Use + to join strings and variables inside println.
public class ConcatDemo {
public static void main(String[] args) {
String browser = "Chrome";
int version = 120;
System.out.println("Browser: " + browser + " v" + version);
int a = 5;
int b = 3;
System.out.println("Sum: " + (a + b)); // Sum: 8
System.out.println("Sum: " + a + b); // Sum: 53 — string concat, not addition
}
}"Sum: " + a + b produces "Sum: 53" because Java evaluates left to right. Once it hits a String, + becomes concatenation. Use parentheses: "Sum: " + (a + b).
| Escape | Output | Use Case |
|---|---|---|
| \n | New line | Multi-line output |
| \t | Tab | Aligned columns |
| \\" | Double quote | Quotes in strings |
| \\\\ | Backslash | File paths on Windows |
public class EscapeDemo {
public static void main(String[] args) {
System.out.println("Status:\tPASS");
System.out.println("Status:\tFAIL");
System.out.println("Path: C:\\Users\\test\\data.csv");
System.out.println("He said \"run the tests\"");
}
}Q: What is the difference between println, print, and printf?
A: println prints text and adds a newline. print prints text without a newline — the next output continues on the same line. printf allows formatted output using placeholders like %s (string), %d (integer), %f (float) for inserting variables into a template string.
Exercise 1: Write a program that prints your name, city, and role — each on a separate line.
Exercise 2: Using printf, print a test summary in this format: Test: LoginTest | Result: PASS | Duration: 2.45s — all values should come from variables.
Exercise 3: What does this print? Figure it out before running.
System.out.println("A" + 1 + 2);
System.out.println(1 + 2 + "A");