-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrinting.java
More file actions
43 lines (32 loc) · 1.33 KB
/
Copy pathPrinting.java
File metadata and controls
43 lines (32 loc) · 1.33 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
public class Printing {
public static void main(String[] args) {
int x = 10, y = 20;
System.out.println("Sum is " + x + y); // prints Sum is 1020
System.out.println("Sum is " + (x + y));// prints Sum is 30
System.out.println("Sum of " + x + " and " + y + " is " + (x + y)); // formatted output basic
int a = 1023;
float b = 13.99f;
char c = 'H';
String str = "Hola";
System.out.printf("Hello %d %f %c %s World\n", a, b, c, str);
System.out.printf("Hello %o %e %c %s World\n", a, b, c, str);
System.out.printf("Hello %x %g %c %s World\n", a, b, c, str);
// Argument index
// General Syntax %[Argument index$][flag][width][.precision]conversion
System.out.printf("%1$d %1$d %1$d\n", x);
System.out.printf("%1$d %2$f %1$d\n", x, b);
System.out.printf("%3$s %1$d %2$c", a, c, str);
// flags and width
int p = 10, q = -11;
System.out.printf("%5d\n", p);
System.out.printf("%05d\n", p);
System.out.printf("%(5d\n", q);
System.out.printf("%+5d\n", q);
float f = 123.457f;
System.out.printf("%6.2f\n", f);
String s = "Java";
System.out.printf("%10s\n", s);
System.out.printf("%-10s\n", s);
// printf and format works same
}
}