printf Basics
Format strings and arguments.
printf Basics is a free Java Academy lesson on CoddyKit — lesson 1 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.
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");
}
}printf Does Not Add a Newline
Unlike println, printf does NOT add a line break automatically. If you want the cursor to move to the next line, add \n yourself.
Forget it and two prints run together on one line.
public class Main {
public static void main(String[] args) {
System.out.printf("A");
System.out.printf("B\n");
}
}The %s Specifier
%s is the string placeholder. It works for text and also for most objects, which Java converts to text automatically.
You can use several %s slots in one call. Values are matched left to right.
public class Main {
public static void main(String[] args) {
System.out.printf("%s likes %s\n", "Ana", "Java");
}
}The %d Specifier
%d formats a whole number (an int or long). The d stands for "decimal integer".
Passing a decimal value like 3.5 to %d causes an error, so keep %d for integers only.
public class Main {
public static void main(String[] args) {
int age = 25;
System.out.printf("Age: %d\n", age);
}
}Multiple Placeholders
You can mix specifiers freely. Each placeholder consumes the next argument in order.
Below, %s takes the name and %d takes the score.
public class Main {
public static void main(String[] args) {
System.out.printf("%s scored %d points\n", "Leo", 42);
}
}Order Matters
Arguments fill placeholders in the exact order they appear. The first value goes to the first %, the second value to the second, and so on.
Swap the values and the output changes meaning.
public class Main {
public static void main(String[] args) {
System.out.printf("%d before %d\n", 1, 2);
}
}The %% Escape
Since % starts a placeholder, how do you print a literal percent sign? Use %%.
One %% in the format string becomes a single % in the output.
public class Main {
public static void main(String[] args) {
System.out.printf("%d%% done\n", 80);
}
}Common Escapes
Inside the format string you can use the usual escape sequences: \n for a newline and \t for a tab.
These help separate values or move to new lines neatly.
public class Main {
public static void main(String[] args) {
System.out.printf("Name:\tBob\nCity:\tRome\n");
}
}printf vs println
println is great for quick output of one value. printf shines when you control exactly how values look.
Rule of thumb: reach for printf when you need placeholders, alignment, or decimal control.
Mismatched Types
The specifier must match the value type. Using %d with text, or %s where you expected a number format, can cause errors or odd output.
Always pair %s with text and %d with integers.
Quick Check
Test your printf basics.
Recap
You learned System.out.printf, placeholders %s and %d, the %% escape, and that printf does not add a newline on its own.
Next you will format numbers and decimals with precision.
Frequently asked questions
Is the “printf Basics” lesson free?
Yes — the full text of “printf Basics” 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 “printf Basics”?
Format strings and arguments. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “printf Basics” 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.