Aral Code

Getting Started Lesson 3 of 21

Java Output and Comments — println, print and printf

Print text and numbers with System.out.println, print and printf, use escape characters, format decimals, and write single-line, multi-line and Javadoc comments.

Showing output is the first thing every program does. Java has three printing methods, and knowing when to use each one makes your programs look clean and professional.

println and print

public class Main {
    public static void main(String[] args) {
        System.out.println("Line one");
        System.out.println("Line two");
        System.out.print("Same ");
        System.out.print("line");
        System.out.println();          // just a new line
        System.out.println("Done!");
    }
}

Output

Line one
Line two
Same line
Done!
  • println prints and then moves to a new line.
  • print prints and stays on the same line.

Printing numbers and joining text

public class Main {
    public static void main(String[] args) {
        System.out.println(25 + 15);
        System.out.println("Total: " + 25 + 15);
        System.out.println("Total: " + (25 + 15));
    }
}

Output

40
Total: 2515
Total: 40

+ adds numbers, but once text is involved it joins (concatenates). Java reads left to right: "Total: " + 25 becomes text, then + 15 joins "15". Parentheses force the addition to happen first.

Escape characters

public class Main {
    public static void main(String[] args) {
        System.out.println("Name:\tJuan");
        System.out.println("Line 1\nLine 2");
        System.out.println("She said \"Kaya natin!\"");
        System.out.println("C:\\Users\\Juan");
    }
}

Output

Name:	Juan
Line 1
Line 2
She said "Kaya natin!"
C:\Users\Juan

printf: formatted output

printf lets you put values into a template with format specifiers:

public class Main {
    public static void main(String[] args) {
        String name = "Maria";
        int grade = 10;
        double average = 92.4567;

        System.out.printf("Name: %s%n", name);
        System.out.printf("Grade %d, average %.2f%n", grade, average);
        System.out.printf("Price: ₱%,.2f%n", 1234567.5);
        System.out.printf("|%-10s|%5d|%n", "Ana", 88);
    }
}

Output

Name: Maria
Grade 10, average 92.46
Price: ₱1,234,567.50
|Ana       |   88|
SpecifierFor
%sText (String)
%dWhole numbers (int, long)
%f, %.2fDecimals; .2 means 2 decimal places
%,.2fDecimals with thousands separators
%nNew line
%-10s, %5dWidth: left-aligned in 10 spaces, right-aligned in 5

String.format(...) works the same way but returns the text instead of printing it.

Comments

/*
 * Program: Grade Calculator
 * Author:  Juan Dela Cruz, Grade 11 - ICT
 */
public class Main {
    /**
     * The starting point of the program.
     * This is a Javadoc comment.
     */
    public static void main(String[] args) {
        // Print a greeting (single-line comment)
        System.out.println("Hello!"); // comments can follow code
        // System.out.println("This line is switched off");
    }
}

Output

Hello!
  • // comments out the rest of the line.
  • /* ... */ comments out everything between them, across many lines.
  • /** ... */ is a Javadoc comment, used to generate documentation for classes and methods.

A receipt with printf

public class Main {
    public static void main(String[] args) {
        System.out.println("===== TINDAHAN NI ALING NENA =====");
        System.out.printf("%-20s %3s %8s%n", "Item", "Qty", "Amount");
        System.out.printf("%-20s %3d %8.2f%n", "Pandesal", 10, 25.0);
        System.out.printf("%-20s %3d %8.2f%n", "Kape 3-in-1", 4, 32.0);
        System.out.printf("%-20s %3d %8.2f%n", "Itlog", 6, 54.0);
        System.out.println("----------------------------------");
        System.out.printf("%-24s %8.2f%n", "TOTAL", 111.0);
    }
}

Output

===== TINDAHAN NI ALING NENA =====
Item                 Qty   Amount
Pandesal              10    25.00
Kape 3-in-1            4    32.00
Itlog                  6    54.00
----------------------------------
TOTAL                      111.00

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does System.out.println("Sum: " + 2 + 3); print?
2. Which method does NOT move to a new line after printing?
3. Which format prints 3.14159 as 3.14?
4. Which is a Javadoc comment?

By Sir Reyel · Last updated 2026-12-04