AI Hints
Home›Java Tutorial›The main Method, println and print, and Comments
Java · Lesson 8 of 120

println and print

Show values on screen.

⏱ 2 min read▶ Video at 9:51🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

System dot out dot println is the line you'll type most in this course. It shows a value, then moves to a new line. There's also print, which doesn't.

  • println: value + new line
  • print: no new line
  • Used everywhere

println versus print

Println prints, then moves to the next line. Print stays on the same line. So these three pieces end up together. An empty println just prints a blank line. Run it. Same line here, appears as one line, followed by an empty line.

Main.javajava
public class Main {
    public static void main(String[] args) {
        System.out.println("One line");
        System.out.println("Another line");
        System.out.print("Same ");
        System.out.print("line ");
        System.out.println("here");
        System.out.println();
        System.out.println("After an empty line");
    }
}
Output
One line
Another line
Same line here

After an empty line

Printing values

Println can print numbers, decimals, and true or false, not only text. But watch the plus sign. After text, plus joins, so ten and five become one zero five. Brackets fix it, by adding first. Run it. The first total is one zero five, and the second is fifteen.

Main.javajava
public class Main {
    public static void main(String[] args) {
        System.out.println(42);
        System.out.println(3.5);
        System.out.println(true);
        System.out.println("Total: " + 10 + 5);
        System.out.println("Total: " + (10 + 5));
    }
}
Output
42
3.5
true
Total: 105
Total: 15

Reading System dot out dot println

System is a built in class. Out is the standard output, usually your terminal. Println is the method that prints. The dots connect each part to the next.

keyvalue
Systema built-in class
outthe terminal output
printlnprint + new line
Shortcuttype sout in most IDEs

Summary

Println prints and moves to a new line, and print stays on the same line. Use brackets when adding numbers inside text. Next, comments.

Key takeaways
  • println: new line
  • print: same line
  • Brackets for sums
Next up: Comments — notes for humans