0Pricing
Java Academy · Lesson

String.format

Build formatted strings.

String.format is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Format Without Printing

printf sends text straight to the console. But sometimes you want the formatted text as a String to store, return, or reuse.

String.format does exactly that: same rules, but it returns a value instead of printing.

Your First String.format

String.format uses the same placeholders as printf. It builds a new string and hands it back to you.

Here the result is stored in msg and then printed.

public class Main {
    public static void main(String[] args) {
        String msg = String.format("Hello, %s!", "World");
        System.out.println(msg);
    }
}

Same Specifiers

Everything you learned applies: %d, %f, %.2f, widths, and flags all work in String.format.

The only difference is that the result is returned, not displayed.

public class Main {
    public static void main(String[] args) {
        String s = String.format("%s: %.2f", "Price", 9.5);
        System.out.println(s);
    }
}

Storing the Result

Because it returns a String, you can keep it in a variable, concatenate it, or pass it to another method.

This is useful for building messages step by step.

public class Main {
    public static void main(String[] args) {
        String label = String.format("#%03d", 7);
        System.out.println("ID " + label);
    }
}

Returning Formatted Text

A method can use String.format to return a tidy string. This keeps formatting logic in one place.

Below, greet returns the formatted greeting.

public class Main {
    static String greet(String name) {
        return String.format("Welcome, %s!", name);
    }
    public static void main(String[] args) {
        System.out.println(greet("Mia"));
    }
}

No Automatic Newline

Like printf, String.format does not add a newline unless you write \n in the format string.

If you print the result with println, that adds the line break for you.

public class Main {
    public static void main(String[] args) {
        String s = String.format("Line one");
        System.out.println(s);
    }
}

Multiple Values

You can pass as many arguments as there are placeholders. They fill the slots left to right, just like printf.

This makes it easy to build a row of data as a single string.

public class Main {
    public static void main(String[] args) {
        String row = String.format("%s | %d | %.1f", "Tea", 3, 2.5);
        System.out.println(row);
    }
}

Reusing a Format

Since the format string is just text, you can apply it many times with a loop to produce consistent strings.

Each call returns a fresh string with new values.

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            System.out.println(String.format("Item %d", i));
        }
    }
}

printf Uses format Internally

Under the hood, System.out.printf(fmt, args) behaves like printing String.format(fmt, args).

So choosing between them is really about whether you want to print now or keep the text for later.

Building Logs and Labels

String.format is handy for log lines, UI labels, and file names where you need a precise pattern.

Centralizing the format means one change updates every message.

public class Main {
    public static void main(String[] args) {
        int code = 404;
        String log = String.format("[ERROR %d] not found", code);
        System.out.println(log);
    }
}

Same Type Rules Apply

The specifier-to-value rules are identical: %d needs an integer, %f needs a floating-point value, and a mismatch throws an exception at runtime.

Double-check pairings before you run.

Quick Check

How does String.format differ from printf?

Recap

String.format shares printf's rules but returns a String you can store, return, or reuse.

Next you will combine widths and alignment to build neat tables.

Frequently asked questions

Is the “String.format” lesson free?

Yes — the full text of “String.format” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “String.format”?

Build formatted strings. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “String.format” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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