0Pricing
Java Academy · Lesson

printf Basics

Format strings and arguments.

Why printf?

Sometimes System.out.println is not enough. You may want numbers lined up, decimals trimmed, or values inserted into a sentence.

System.out.printf lets you build a formatted string using placeholders. It is short for "print formatted".

Your First printf

A printf call takes a format string plus values. Each placeholder begins with %.

Here %s means "insert a string here". The value after the comma fills that slot.

public class Main {
    public static void main(String[] args) {
        System.out.printf("Hello, %s!\n", "World");
    }
}

All lessons in this course

  1. printf Basics
  2. Number and Decimal Formats
  3. String.format
  4. Aligning Tables
← Back to Java Academy