-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuncInter.java
More file actions
34 lines (29 loc) · 852 Bytes
/
FuncInter.java
File metadata and controls
34 lines (29 loc) · 852 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
26
27
28
29
30
31
32
33
34
// ELX-FuncInter/FuncInter.java
@FunctionalInterface
interface MyInterface<T> {
int len(T t);
}
public class FuncInter {
static <T> int calc(T arg, MyInterface<T> f) {
return f.len(arg);
}
public static void main (String[] args) {
String s = "Alice";
int result1 = calc(
s,
new MyInterface<String>() {
@Override
public int len(String s) {
return s.length();
}
});
System.out.println("result1 = " + result1);
Double d = 123.456; // boxing
int result2 =
calc(d, v -> v.toString().length());
System.out.println("result2 = " + result2);
int result3 =
calc(d, v -> (int)(1000*v+4));
System.out.println("result3 = " + result3);
}
}