Java Output Using System.out

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.
  • System is a built-in class; out is the output channel inside it. So System.out means โ€œthe output channel of the systemโ€.
  • You import nothing. System is always available, so System.out is 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.

  • println adds a new line after the text, so the next print starts fresh below.
  • print does 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 print calls stay on the same line, so โ€œHello โ€ and โ€œthereโ€ join up.
  • The empty println() moves to the next line.
  • Each println after that prints its text and drops to a new line, so โ€œLine oneโ€ and โ€œLine twoโ€ each get their own line.

Output

Hello there
Line one
Line two

The 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:

  • %.2f is a decimal number with two digits after the dot.
  • %s is a String. %n is a new line.
  • Java fills each placeholder, in order, with the value you pass after the format string.
  • printf and format are two names for one method.

Output

Price: 9.50
Name: Alex

For 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, a double, a boolean, 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 42 prints as 42.
  • The boolean true prints as the word true.
  • The String prints as its plain text.

Output

42
19.99
true
Alex

What 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 90

Now 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 + 2 are numbers, so they add to 3; then 3 + "x" hits text and joins to 3x.
  • Second line: "x" + 1 starts with text, so it joins to x1; then x1 + 2 joins to x12.
  • Same values, different answer, all because of order.

Output

3x
x12

So 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: 12
System.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:

  • \n is a new line, so the text splits onto two lines.
  • \t is 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 one
Line two
Name: Alex
She said "hi"
Path: C:\Users

The 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 to err.

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; use System.err only 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: 53
System.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 breaks
System.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 println for separate lines, and print when 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 on
System.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, printf with %.2f is cleaner than joining by hand.
  • Send errors to System.err. Keep normal results on System.out and real error messages on System.err, so they can be handled separately.

๐Ÿงฉ What Youโ€™ve Learned

Nice work. Here are the key points.

  • โœ… System.out is the standard output stream, and you do not import it.
  • โœ… println adds a new line, print does not, and printf prints with a format string.
  • โœ… println can print any type, turning it into text for you, and objects print via toString().
  • โœ… The + symbol adds numbers but joins text, so order matters: 1 + 2 + "x" is 3x, but "x" + 1 + 2 is x12.
  • โœ… Escape sequences like \n, \t, \" and \\ put special characters inside text.
  • โœ… System.err is a separate stream meant for error messages.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 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. 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. 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. 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.

Java Scanner Class

Share & Connect