Skip to content

Commit a2ed984

Browse files
committed
feat(OperatorInterfaceDemo): add demo using UnaryOperator and BinaryOperator with apply()
What - Added OperatorInterfaceDemo class demonstrating usage of java.util.function.UnaryOperator and BinaryOperator. - Defined: - UnaryOperator<Integer> a → multiplies input by 2. - BinaryOperator<Integer> b → adds two integers. - Executed both with apply(): - a.apply(5) → 10. - b.apply(5, 5) → 10. - Printed results with descriptive messages. Why - Shows how to execute lambda expressions stored in operator interfaces. - Clarifies difference between printing operator reference vs executing with apply(). - Educational example highlighting the purpose of UnaryOperator and BinaryOperator as specializations of Function and BiFunction. How - Declared lambdas for arithmetic operations. - Invoked apply() method on both operator instances. - Printed results to stdout. Logic - Inputs: integer 5 for unary; integers 5 and 5 for binary. - Outputs: - "Unary result (2 * 5): 10". - "Binary result (5 + 5): 10". - Flow: 1. a.apply(5) → 2 * 5 → 10. 2. b.apply(5, 5) → 5 + 5 → 10. 3. Print results. - Edge cases: trivial arithmetic; no exceptions expected. - Complexity / performance: O(1). - Concurrency / thread-safety: lambdas are stateless, safe across threads. - Error handling: not applicable. Real-life applications - UnaryOperator: transformations like scaling numbers, normalizing data, incrementing values. - BinaryOperator: aggregation (sum, min, max), often used with Stream.reduce(). - Useful in mathematical pipelines, configuration merging, or data combination scenarios. Notes - Demonstrates execution with apply(); without it, only the object reference prints. - Cleaner than earlier version since no unused methods present. - Method references (Integer::sum, x -> x * 2 as inline) can further simplify usage in production code. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent b021236 commit a2ed984

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

JAVA8/Operator/src/OperatorInterfaceDemo.java renamed to Java 8 Crash Course/Unary operators and Binary operators/src/OperatorInterfaceDemo.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ public static void main(String[] args) {
1212
// BinaryOperator: adds two integers
1313
BinaryOperator<Integer> b = (x, y) -> x + y;
1414

15-
// actual results, call apply() on the operators.
15+
// for actual results, call apply() on the operators.
1616
System.out.println("Unary result (2 * 5): " + a.apply(5));
1717
System.out.println("Binary result (5 + 5): " + b.apply(5, 5));
1818
}
1919
}
2020

2121
/*
2222
Lambda Expression Execution:
23-
Use the apply method (for both UnaryOperator and BinaryOperator) to execute the lambda and obtain a result.
23+
Use the apply method (for both UnaryOperator and BinaryOperator) to execute the lambda and obtain a
24+
result.
2425
25-
Simply printing the lambda variable (e.g., System.out.println(a);) shows the reference, not the computed value.
26+
Simply printing the lambda variable (e.g., System.out.println(a);) shows the reference,
27+
not the computed value.
2628
2729
Unused Methods:
2830
In Version 1, the sum method is defined but not used.
2931
Version 2 is a cleaner example if you want to focus only on lambda expressions.
30-
*/
32+
*/

0 commit comments

Comments
 (0)