โ
How to Display Text with printf
in Java
Basic Syntax
-
formatString
contains text with format specifiers. -
arguments
are values you want to insert into the text.
Common Format Specifiers
Specifier | Meaning | Example |
---|---|---|
%s |
String | "Hello %s" |
%d |
Integer | "Number: %d" |
%f |
Floating-point number | "Price: %.2f" |
%n |
New line (platform independent) |
๐ข Example: Display formatted text
๐ข Output
Tips:
-
%n
is better than\n
because it works across OSes. -
Use
%.2f
to show 2 decimal places for floats/doubles.