W3Schools Java printf Tutorial – Master String Formatting Like a Pro

Robert Jul 13, 2026

Java's 'printf' function provides an efficient way to format and display data, analogous to C's printf. It allows inserted values to be placed in the text using placeholders, known as format specifiers. Let's delve into how 'printf' works in Java.

How to print in Java?
How to print in Java?

Java's 'printf' is part of the java.util.Formatter class and is inherited from the Object class. It behaves differently compared to C's printf. In Java, the 'printf' method is more than just a function, it's a class with multiple formatting options.

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀

Basic Syntax and Formatting

'printf' follows the general syntax: printf(format, arguments);: format specifies the layout of the output, while arguments provide the data to print.

leap year program in java language for btech students in a easy approach.
leap year program in java language for btech students in a easy approach.

Format specifiers start with a '%', followed by flags that modify its behaviour. The basic structure is:

  1. '%' - Introduces the format specifier.
  2. 'flag(s)' - Optional flags that alter the output. Common flags include: '-', '+', ' ', and '#'.
  3. 'precision' - A decimal number that dictates output precision.
  4. 'conversion' - Specifies the type of data being converted: 'd', 's', 'c', etc.
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻

Data Types

'printf' supports various data types. Each data type is represented by a specific 'conversion'. Some of the most common are:

  • '%c' - Character.
  • '%d', '%o', '%x' - Integers in decimal, octal, and hexadecimal bases respectively.
  • '%f', '%e', '%g' - Floating-point numbers in fixed, scientific, and general formats.
  • '%s' - String.
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide

For instance, to print the statement "Hello, World!" and the integer 42, you would use: printf("Hello, %s! %d\n", "World", 42);

Width and Precision

'printf' uses width and precision to control the output's appearance. Width is specified with an integer and indicates the minimum field width. If the value has fewer characters, it will be padded with spaces on the left or spaces/zeros on the right (when using widths with decimal numbers).

Java Cheatsheet
Java Cheatsheet

Precision is specified with a decimal and dictates the maximum output field size or the number of decimal places to display. For floating-point numbers, it sets the number of digits after the decimal point.

Advanced Features

a black and white graduation hat holding a red book with a cup of coffee in it
a black and white graduation hat holding a red book with a cup of coffee in it
80+ Pattern Programs In Java
80+ Pattern Programs In Java
Object in java
Object in java
java at a glance 😎
java at a glance 😎
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
an image of a computer screen with the words it, triangular pattern on it and a black background
an image of a computer screen with the words it, triangular pattern on it and a black background
the school logo is shown on a green square with white letters that spell out 3
the school logo is shown on a green square with white letters that spell out 3
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
Interface in Java
Interface in Java
two different types of words that appear to be in the same language, one is for each
two different types of words that appear to be in the same language, one is for each
Welcome to Java Programming for Kids: Visual Lessons and Projects
Welcome to Java Programming for Kids: Visual Lessons and Projects
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀
w3schools java printf
w3schools java printf
Java Switch Example
Java Switch Example
a poster with an image of a programming program
a poster with an image of a programming program
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
Best Java IDEs in 2020
Best Java IDEs in 2020
🌟 ☕Java User Input Example | Learn Java Programming Easily
🌟 ☕Java User Input Example | Learn Java Programming Easily
Java OOP Concepts Explained for Beginners | Crack Interviews Easily
Java OOP Concepts Explained for Beginners | Crack Interviews Easily

Java's 'printf' supports advanced features that allow for more flexible and complex formatting. These include:

'n' Formatting

'%tN' Method Invocation

This feature allows objects to be formatted by calling a specific method of the object, rather than relying on the standard conversion methods.

Understanding and leveraging the full power of 'printf' in Java can greatly enhance your output formatting capabilities. Practice and experimentation are key to mastering this versatile tool.

From here, you're encouraged to explore the Java 'printf' method further, try it out in your own programs, and see how you can integrate it into your coding workflow.