Java Output Using System.out
Table of Contents + โ
In the last lesson you learned about the Java ternary operator. You have been printing things since your first program. But output hides a few surprises. Letโs clear them up.
๐ค What is System.out?
You print to the standard output stream, and Java gives it to you as System.out.
- A stream is a channel that carries data; the standard output stream carries text to the screen.
Systemis a built-in class;outis the output channel inside it. SoSystem.outmeans โthe output channel of the systemโ.- You import nothing.
Systemis always available, soSystem.outis ready the moment your program starts. - Everything you print lands in the terminal, or the console panel in your editor.
๐จ๏ธ print vs println
There are two basic ways to print. The difference is what happens at the end.
printlnadds a new line after the text, so the next print starts fresh below.printdoes not. It leaves the cursor right where the text ended.
Here both are used side by side.
public class PrintDemo { public static void main(String[] args) { System.out.print("Hello "); System.out.print("there"); System.out.println(); System.out.println("Line one"); System.out.println("Line two"); }}What each line does:
- The two
printcalls stay on the same line, so โHello โ and โthereโ join up. - The empty
println()moves to the next line. - Each
printlnafter that prints its text and drops to a new line, so โLine oneโ and โLine twoโ each get their own line.
Output
Hello thereLine oneLine twoThe rule is short: use println for each piece on its own line, and print to keep building text on the same line, like a prompt before reading input.
๐ค printf and format
A third option, printf, prints with a format string.
- A format string is a template with placeholders that you fill with values.
- It is handy for clean, lined-up output, like a fixed number of decimal places.
- A later lesson covers it fully, so here is just a taste.
This prints a price with exactly two decimal places.
public class FormatDemo { public static void main(String[] args) { double price = 9.5; System.out.printf("Price: %.2f%n", price); System.out.format("Name: %s%n", "Alex"); }}What the placeholders mean:
%.2fis a decimal number with two digits after the dot.%sis a String.%nis a new line.- Java fills each placeholder, in order, with the value you pass after the format string.
printfandformatare two names for one method.
Output
Price: 9.50Name: AlexFor now, just know printf exists for formatted output. Most of the time print and println are all you need.
๐งฎ Printing different types
System.out.println can print almost any value you give it.
- It handles an
int, adouble, aboolean, a String. - Java turns the value into text for you, then prints it.
- So you never convert numbers to text by hand before printing.
Here is one of each type printed in turn.
public class TypeDemo { public static void main(String[] args) { int count = 42; double price = 19.99; boolean isReady = true; String name = "Alex";
System.out.println(count); System.out.println(price); System.out.println(isReady); System.out.println(name); }}Each println makes a text version of the value and prints it:
- The number
42prints as42. - The boolean
trueprints as the wordtrue. - The String prints as its plain text.
Output
4219.99trueAlexWhat about your own objects?
- Printing an object prints whatever its
toString()method returns. - Every object has one. The default is not readable, usually the class name and a code.
- So a plain object looks messy until you give it a proper
toString()(covered in the objects lessons).
โ Concatenation and the + gotcha
The + symbol joins text and values. When one side is a String, Java turns the other side into text and sticks them together. This joining is called concatenation.
Here a label and a number are joined.
public class JoinDemo { public static void main(String[] args) { int score = 90; System.out.println("Your score is " + score); }}The left side is text, so Java turns score into the text 90 and joins it on. The result is one String, then printed.
Output
Your score is 90Now the surprise. + does two jobs, and Java reads left to right, so order changes the result:
- With numbers,
+adds. With text, it joins. - Java works through a line from left to right, one step at a time.
- These two lines use the same pieces in a different order.
public class OrderDemo { public static void main(String[] args) { System.out.println(1 + 2 + "x"); System.out.println("x" + 1 + 2); }}Step through both:
- First line:
1 + 2are numbers, so they add to3; then3 + "x"hits text and joins to3x. - Second line:
"x" + 1starts with text, so it joins tox1; thenx1 + 2joins tox12. - Same values, different answer, all because of order.
Output
3xx12So when you mix numbers and text, watch the order. To get a real sum, do the math first inside brackets. Brackets force Java to calculate before any joining happens.
System.out.println("Total: " + 1 + 2); // โ prints Total: 12System.out.println("Total: " + (1 + 2)); // โ
prints Total: 3๐ฃ Escape sequences
Some characters are hard to put inside text directly: a new line, a tab, a double quote (which would end the String). For these you use an escape sequence, a backslash followed by a letter or symbol that stands for a special character.
Here are the common ones in action.
public class EscapeDemo { public static void main(String[] args) { System.out.println("Line one\nLine two"); System.out.println("Name:\tAlex"); System.out.println("She said \"hi\""); System.out.println("Path: C:\\Users"); }}What each one does:
\nis a new line, so the text splits onto two lines.\tis a tab, which pushes โAlexโ across with a gap.\"puts a quote mark inside the text without ending the String.\\prints a single backslash, since one backslash alone would start a new escape sequence.
Output
Line oneLine twoName: AlexShe said "hi"Path: C:\UsersThe backslash is the signal that says โtreat the next character in a special wayโ.
๐จ System.out vs System.err
There is a second output stream, System.err, the standard error stream.
- It works like
System.out, but it is meant for error messages, not normal output. - Keeping the two apart lets tools handle them separately: results go to
out, problems go toerr.
Here both are used.
public class StreamDemo { public static void main(String[] args) { System.out.println("All good, here is your result."); System.err.println("Something went wrong."); }}Why this matters:
- On most terminals the two look the same, but they travel through different channels.
- In real projects you can save normal output to a file while still seeing errors on screen.
- For everyday printing, stick with
System.out; useSystem.erronly for actual error messages.
Output
All good, here is your result.Something went wrong.โ ๏ธ Common Mistakes
A few output errors trip people up. Catch them early.
- Using print when you meant println. With
print, the next output runs straight on, with no line break. That surprises people when lines join up unexpectedly.
System.out.print("Item one");System.out.print("Item two"); // โ prints "Item oneItem two" on one line
System.out.println("Item one");System.out.println("Item two"); // โ
each on its own line- Wrong concatenation order with numbers. Mixing math and joining in the wrong order gives glued digits instead of a sum.
System.out.println("Sum: " + 5 + 3); // โ prints Sum: 53System.out.println("Sum: " + (5 + 3)); // โ
prints Sum: 8- Forgetting to escape special characters. A bare quote inside text ends the String early and breaks your code.
System.out.println("She said "hi"); // โ the second quote ends the String, code breaksSystem.out.println("She said \"hi\""); // โ
escaped quotes stay inside the textโ Best Practices
Build these habits to keep output clean and correct.
- Pick print or println on purpose. Use
printlnfor separate lines, andprintwhen you want text to continue on the same line. - Wrap math in brackets when joining. When a sum sits next to text, put it in brackets so Java adds before it joins.
System.out.println("Age next year: " + age + 1); // โ glues the 1 onSystem.out.println("Age next year: " + (age + 1)); // โ
adds first, then joins- Use printf for lined-up numbers. When you need a fixed number of decimal places,
printfwith%.2fis cleaner than joining by hand. - Send errors to System.err. Keep normal results on
System.outand real error messages onSystem.err, so they can be handled separately.
๐งฉ What Youโve Learned
Nice work. Here are the key points.
- โ
System.outis the standard output stream, and you do not import it. - โ
printlnadds a new line,printdoes not, andprintfprints with a format string. - โ
printlncan print any type, turning it into text for you, and objects print viatoString(). - โ
The
+symbol adds numbers but joins text, so order matters:1 + 2 + "x"is3x, but"x" + 1 + 2isx12. - โ
Escape sequences like
\n,\t,\"and\\put special characters inside text. - โ
System.erris a separate stream meant for error messages.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is the difference between print and println?
Why: println prints the text and then moves to a new line, while print leaves the cursor on the same line.
- 2
What does System.out.println(1 + 2 + "x") print?
Why: Java reads left to right. 1 + 2 are numbers so they add to 3, then 3 + "x" joins to make 3x.
- 3
What does System.out.println("x" + 1 + 2) print?
Why: The left side "x" is text, so 1 joins to make x1, then 2 joins to make x12. No addition happens.
- 4
Which escape sequence prints a tab?
Why: \t is the tab escape sequence. \n is a new line, \" is a quote, and \\ is a single backslash.
๐ Whatโs Next?
You can show output clearly now. Next, letโs go the other way and read what the user types, so your program can react to real input.