-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormattedOutput.java
More file actions
102 lines (92 loc) · 3.5 KB
/
Copy pathFormattedOutput.java
File metadata and controls
102 lines (92 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package Phase0_SetupAndFirstPrograms.Output;
/**
* <h1>Advanced Formatted Output</h1>
*
* <p>Beyond plain <code>printf</code>, Java offers two more ways to produce
* formatted text:</p>
*
* <ul>
* <li><code>String.format(...)</code> — returns the formatted string (does not print).</li>
* <li><code>java.util.Formatter</code> — the lower-level engine behind <code>printf</code> and <code>format</code>.</li>
* <li>Text Blocks (Java 15+) — multi-line string literals using """ delimiters.</li>
* </ul>
*
* <h2>Common Format Specifiers (Recap)</h2>
* <pre>
* %d - decimal integer
* %o - octal integer
* %x - hexadecimal integer (use %X for uppercase)
* %e - scientific notation
* %f - floating point
* %g - general floating point (chooses %e or %f)
* %s - string (calls toString() for objects)
* %c - character
* %b - boolean
* %t - date/time (followed by a sub-specifier like %tY for year)
* %n - platform-specific line separator
* </pre>
*
* <h2>Argument Index</h2>
* <p>You can reference the same argument multiple times using "1$", "2$", etc.</p>
* <pre>
* "%1$s is %1$s years old" -> reuses argument #1 twice
* </pre>
*
* @author Deepak Gupta
* @version 1.0
* @since 2026-05-21
*/
public class FormattedOutput {
public static void main(String[] args) {
// --- 1) String.format - capture formatted text into a String ---
String message = String.format("User %s scored %d/%d (%.1f%%)",
"Deepak", 87, 100, 87.0);
System.out.println(message);
// --- 2) Integer in different bases ---
int n = 255;
System.out.printf("Decimal: %d%n", n); // 255
System.out.printf("Octal : %o%n", n); // 377
System.out.printf("Hex : %x%n", n); // ff
System.out.printf("HEX : %X%n", n); // FF
// --- 3) Scientific notation ---
double avogadro = 6.022e23;
System.out.printf("%e%n", avogadro); // 6.022000e+23
System.out.printf("%.2e%n", avogadro); // 6.02e+23
// --- 4) Argument indexing - reuse the same value ---
System.out.printf("%1$s loves %2$s, and %2$s loves %1$s.%n", "Java", "you");
// --- 5) Date / Time formatting ---
java.util.Date now = new java.util.Date();
System.out.printf("Year=%tY Month=%tm Day=%td%n", now, now, now);
System.out.printf("Time=%tH:%tM:%tS%n", now, now, now);
// --- 6) Text Blocks (Java 15+) - multi-line strings ---
String json = """
{
"name" : "Deepak",
"age" : 25,
"skills": ["Java", "Spring", "SQL"]
}
""";
System.out.println(json);
// --- 7) String.formatted (instance method, Java 15+) - same as String.format ---
String greeting = "Hello %s, welcome to %s!".formatted("Deepak", "Java");
System.out.println(greeting);
// SAMPLE OUTPUT
// User Deepak scored 87/100 (87.0%)
// Decimal: 255
// Octal : 377
// Hex : ff
// HEX : FF
// 6.022000e+23
// 6.02e+23
// Java loves you, and you loves Java.
// Year=2026 Month=05 Day=19
// Time=12:30:45
// {
// "name" : "Deepak",
// "age" : 25,
// "skills": ["Java", "Spring", "SQL"]
// }
//
// Hello Deepak, welcome to Java!
}
}