String Formatting
What is String Formatting?
We format strings in Java for clarity, control, and consistency. It enhances code readability, allows us to precisely present data (numbers, dates, etc.), and ensures consistent output across the application. This improves maintainability, user experience, and sometimes even efficiency.
String Specifiers
We use specifiers to be a placeholder of where a data should be placed in a String.
Specifier
Explanation
%s
Converts the data attached automatically to a String equivalent data
%d
Used for Integers, allows the placement of integers within a string
%f
Used for decimals/floats/doubles. This allows decimal values to be represented in a string. You can also control the decimal place.
Formatting a string
Formatting numbers:
Formatting numbers with decimals:
Using multiple arguments:
Last updated