forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhatHappensPrintf.java
More file actions
25 lines (21 loc) · 952 Bytes
/
WhatHappensPrintf.java
File metadata and controls
25 lines (21 loc) · 952 Bytes
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
public class WhatHappensPrintf {
/**
Exercise 3.1 When you use printf, the Java compiler does not check your
format string. See what happens if you try to display a value with type int
using %f. And what happens if you display a double using %d? What if you
use two format specifiers, but then provide only one value?
**/
public static void main (String[] arguments) {
final int x = 10;
final double y = 3.25;
//print int with %f
System.out.printf("Print int with floating point format mask %f", x);
// throws java.util.IllegalFormatConversionException
//print double with %d
System.out.printf("Print double with integer format mask %d", y);
// throws java.util.IllegalFormatConversionException
//print with format specifiers but only one argument
System.out.printf("Print with two format specifiers %d and %f, but with only one argument supplied.", x);
// throws java.util.MissingFormatArgumentException
}
}