Print Formatted String with Placeholders
This program prints a string using placeholders.
Problem Statement
Write a Java program that prints variables using formatted placeholders.
Source Code
| 1 | public class FormattedString { |
| 2 | public static void main(String[] args) { |
| 3 | String name = "Eduinq"; |
| 4 | int year = 2026; |
| 5 | System.out.printf("Welcome %s, Year: %d", name, year); |
| 6 | } |
| 7 | } |
Program Output
Welcome Eduinq, Year: 2026
Explanation
The %s placeholder is replaced by the string variable and %d by the integer variable when printf is called, showing how placeholders can format output.