Java uses System.out to write ordinary console output. The print method displays a value and leaves the cursor on the same line. The println method displays a value and then ends the line. Use this distinction to build readable messages, prompts and reports instead of guessing why text joins together.
A console is a text stream rather than a page layout. Java writes characters in the order your statements execute. This lesson follows that sequence and then introduces printf when labels need consistent numeric formatting.
What System.out represents
System is a library class, and its out field provides a PrintStream for standard output. A terminal usually displays that stream, but a command can redirect it to a file. A program should therefore print useful plain text without depending on a particular editor window.
System.out.print accepts several kinds of values, including strings, integers and booleans. Java chooses an appropriate overloaded method from the argument type. For an object, normal printing uses its string representation. You do not need to convert every number to a string before printing it. An empty println call is valid and writes only the line ending.
print keeps the current line open
A print call writes its argument without adding a newline. A later print call continues directly after those characters, even if its statement appears on a separate source-code line. Spaces come from your strings, not from the distance between statements in the source.
For example, printing "Order:" and then 42 produces Order:42. Include a trailing space in the label when you want Order: 42. This behavior is useful for a prompt such as "Enter a quantity: ", because the input can appear on the same line. It also means a long series of print calls may leave the next terminal prompt attached to the final result unless you eventually end the line.
println finishes a line
println writes its argument and then the platform line separator. Calling println twice normally produces two lines, even when the two calls are written on one source-code line. Calling println with no argument produces a blank line.
On different operating systems, a line separator can consist of different characters. For normal console output, let println choose it rather than building a platform-specific separator manually. A blank line can distinguish sections of a report, but many empty println calls make short output harder to scan.
Newlines inside a string
The escape sequence \n represents a newline character inside a Java string. Printing "First\nSecond" therefore displays two lines even with print. The two source characters backslash and n become one newline character when Java interprets the literal.
A println call also adds its own line separator after the argument. If your string already ends with \n, that extra separator creates a blank line. This explains a frequent spacing surprise. Use either an internal newline for a multiline string or separate println calls when possible. A literal backslash requires \\, which is useful when you need to display the characters \n rather than inserting a line break.
Numbers, addition and concatenation
The plus operator performs arithmetic when both operands are numeric. If a string participates, plus concatenates text. Java evaluates a chain of plus operations from left to right, so "Total: " + 2 + 3 produces Total: 23. The expression "Total: " + (2 + 3) produces Total: 5 because parentheses evaluate the addition first.
Keep calculations separate from presentation when the expression becomes difficult to follow. Store the computed total in a clearly named variable, then print that value with its label. This prevents a missing pair of parentheses from silently turning addition into concatenation and makes it easier to test the calculation without inspecting the output wording.
printf controls the presentation
printf uses a format string containing placeholders. The %d placeholder formats an integer, %s formats a string, and %.2f formats a floating-point number with two digits after the decimal separator. The arguments following the format string supply those values in order. Use %n for a platform-appropriate line ending; printf does not add one automatically.
Numeric formatting can depend on the default locale. Specify java.util.Locale.ROOT when an example needs reproducible decimal punctuation. Formatting rounds the displayed value but does not change the stored number. For money calculations, formatting is only presentation; precise arithmetic may require BigDecimal, which is a separate topic.
Practice with explained solutions
Task 1. Print Code, a space, and 27 on one line using two print calls and one println call.
Solution. One solution prints "Code ", then 27, then calls println with no argument. The output line is Code 27.
Task 2. Format 8.125 with two fractional digits and end the line using printf.
Solution. Use Locale.ROOT with "%.2f%n". For this exactly representable value the output is 8.13; the original variable still stores 8.125.
Java Summary
- print keeps subsequent output on the current line; println ends that line.
- printf uses placeholders and an explicit locale for predictable numeric formatting.
- Parenthesize arithmetic before combining its result with a string label.
- Continue with strings and escapes.
Java Examples
Each example shows the code, the output it produces and what each part does.
Build one line from several calls
Save this complete program as OutputSequence.java. Compile with javac --release 17 OutputSequence.java and run with java OutputSequence.
public class OutputSequence {
public static void main(String[] args) {
System.out.print("Seat ");
System.out.print(14);
System.out.println(" reserved");
System.out.println("Thank you");
}
}Seat 14 reserved
Thank youLine-by-line explanation
- Lines 1 to 8The first call writes the label and its trailing space. The second writes 14 immediately after it. The third contributes another space and the word reserved before ending the line. The fourth starts on the next line. Remove the leading space from " reserved" and the characters touch; changing indentation will not restore that missing space.
Control blank lines
Save this complete program as NewlineReport.java. Compile with javac --release 17 NewlineReport.java and run with java NewlineReport.
public class NewlineReport {
public static void main(String[] args) {
System.out.print("North\nSouth\n");
System.out.println("East");
System.out.println();
System.out.println("West");
}
}North
South
East
WestLine-by-line explanation
- Lines 1 to 8The first print contains two newline escape sequences, so East begins after South. The empty println creates the deliberate blank line between East and West. There is no hidden separator inserted between print calls. Reading the output character by character helps explain this result more reliably than counting the number of source statements.
Format a small receipt
Save this complete program as ReceiptOutput.java. Compile with javac --release 17 ReceiptOutput.java and run with java ReceiptOutput.
public class ReceiptOutput {
public static void main(String[] args) {
int items = 3;
double price = 2.5;
System.out.printf(java.util.Locale.ROOT, "Items: %d%n", items);
System.out.printf(java.util.Locale.ROOT, "Unit price: %.2f%n", price);
System.out.printf(java.util.Locale.ROOT, "Total: %.2f%n", items * price);
}
}Items: 3
Unit price: 2.50
Total: 7.50Line-by-line explanation
- Lines 1 to 9The integer argument fills %d, while each floating-point argument fills %.2f. Locale.ROOT keeps the decimal separator consistent for this demonstration. The displayed 2.50 has two fractional digits even though the variable was initialized as 2.5. Each %n ends its line. The multiplication occurs before the result reaches printf, keeping the arithmetic distinct from the formatting.
Common Java Mistakes
printf placeholders that don't match the arguments
A printf format string must match its arguments. Supplying a string to %d can fail at runtime with an IllegalFormatConversionException. Too few arguments can also fail. Check placeholder types and count rather than converting every value indiscriminately. If an ordinary println produces unexpected text, inspect parentheses around arithmetic before changing the label.
Java FAQ
Which method should beginners use most often?
println is a convenient default for complete results. Use print when continuing a line is intentional, and printf when a clear formatting requirement exists.
Why does my next terminal prompt appear beside the output?
Your final print or printf probably omitted a line ending. Add println or include %n in the format string.
What is the difference between System.out and System.err?
System.out is standard output, for a program's normal results. System.err is standard error, for warnings and error messages. Both usually appear in the terminal, but they can be redirected separately, so a user can save results to a file and still see errors on screen.
How do I print quotes, tabs or backslashes?
Use escape sequences inside the string: \" for a double quote, \t for a tab, \\ for a backslash and \n for a new line. System.out.println("Say \"hi\"") prints Say "hi".
Why is printing thousands of lines slow?
Each println call writes to the console separately, and the terminal must draw every line. For large outputs, build the text with a StringBuilder and print it once, or write to a file. For normal programs and debugging, println is perfectly fast enough.
Java Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
What does "Total: " + (2 + 3) print?
Why might a string ending in \n followed by println create an extra blank line?
Java Quiz
2 questions to check what you learned.
What does print add after its argument?
- Nothing automatically
- Always a newline
- Always a space
What does %.2f control?
- Two fractional digits in the displayed number
- The number stored in the variable
- Two integers