-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvJava_Ch19_21_LambdaExpressions.java
More file actions
74 lines (55 loc) · 2.45 KB
/
AdvJava_Ch19_21_LambdaExpressions.java
File metadata and controls
74 lines (55 loc) · 2.45 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
package AdvancedJava;
@FunctionalInterface
interface announcements1{
public void winner();
}
@FunctionalInterface
interface announcements2{
public void loser();
}
@FunctionalInterface
interface announcements3{
public void loser(int noOfTeams);
}
public class AdvJava_Ch19_21_LambdaExpressions {
public static void main(String[] args) {
System.out.println();
/* Anonymous Classes: These are classes which can be made by not actually manually creating a class before but, anonymous classes
creation inside the main function using the implementation of an Interface first.
Syntax:
<interface_name> object = new <Class_to_be_created>{ * all the method inside the interface *};
Benefits: IntelliJ Idea automatically create an anonymous class , so we just have to write a single line of code. */
// Implementation : [ Note: These anonymous classes are made within the main() function ]
announcements1 object = new announcements1() {
@Override
public void winner() {
System.out.println("Congratulations Winners!!");
}
};
// Now implementing the methods using the object created:
object.winner(); System.out.println();
// ************************************** //
/* Lambda Expression: To use methods of an interface we conventionally used to create a class then an object.
This increases our code size and lines:
What if we can use the methods directly in main() method:
* To do so use: Lambda Expressions:
Syntax:
<interface_name> object = ()->{
// Block of Code //
};
object.<method_name>;
******
It enhances the readability of our code and reduce the size of our code.
Note:: Can only be used with functional interface */
announcements2 obj = ()->{
System.out.println("Better Luck next time");
};
obj.loser();
System.out.println();
// Note if a : interface method has a parameter - define it within the parenthesis-() and then call the desired method.
announcements3 obj2 = (noOfTeams)->{
System.out.printf("No. of Teams losing are %d" , noOfTeams);
};
obj2.loser(3);
}
}