Commit 1dd7480
committed
docs(Operator): add explanation and examples of UnaryOperator and BinaryOperator in Java
What
- Documented UnaryOperator and BinaryOperator functional interfaces in Java.
- Included:
1. UnaryOperator:
- Definition: takes one argument, returns a value of the same type.
- Specialized form of Function<T, T>.
- Example: incrementing integer by 1 using UnaryOperator<Integer> increment = x -> x + 1.
- Demonstration: increment.apply(5) → 6.
2. BinaryOperator:
- Definition: takes two arguments of the same type, returns same type.
- Specialized form of BiFunction<T, T, T>.
- Example: adding two integers with BinaryOperator<Integer> add = (x, y) -> x + y.
- Demonstration: add.apply(5, 10) → 15.
3. Usage guidelines:
- Use UnaryOperator<T> for transforming or modifying a single value (increment, uppercase, squaring).
- Use BinaryOperator<T> for combining two values (sum, max, concatenation).
- Summary:
- UnaryOperator<T> → one input, same-type output.
- BinaryOperator<T> → two inputs, same-type output.
- Both are common in stream operations like map and reduce.
Why
- Provides clear and practical explanation of UnaryOperator and BinaryOperator.
- Helps developers understand when to use each in functional programming contexts.
- Bridges theory with working code examples for quick learning.
How
- Explained interfaces and their relationship to Function and BiFunction.
- Provided standalone Java classes with code and output.
- Annotated examples with explanations for clarity.
- Highlighted real use cases: transformation vs combination.
Logic
- Inputs: integers 5 (for unary) and 5,10 (for binary).
- Outputs: 6 for UnaryOperator; 15 for BinaryOperator.
- Flow:
1. Define lambda using operator interface.
2. Call apply() with input(s).
3. Print result.
- Edge cases:
- Operators assume non-null inputs.
- Arithmetic overflow possible for large numbers.
- Complexity / performance: O(1).
- Concurrency / thread-safety: lambdas shown are stateless and thread-safe.
- Error handling: not needed in examples.
Real-life applications
- UnaryOperator:
- Transforming numbers, normalizing strings, incrementing counters.
- Used with Stream.map for element-wise transformations.
- BinaryOperator:
- Summing values, combining strings, computing min/max.
- Used with Stream.reduce to combine elements into one.
Notes
- Prefer method references for readability when possible (e.g., Integer::sum).
- UnaryOperator and BinaryOperator improve clarity compared to generic Function/BiFunction.
- Widely used in functional programming patterns introduced in Java 8.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent a2ed984 commit 1dd7480
File tree
1 file changed
+68
-0
lines changed- Java 8 Crash Course/Unary operators and Binary operators/src
1 file changed
+68
-0
lines changedLines changed: 68 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments