Skip to content

Commit 1dd7480

Browse files
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

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Unary and Binary Operators in Java:
2+
3+
1. Unary Operator
4+
- A unary operator takes a single argument and returns a value of the same type.
5+
- In Java, this is represented by the `UnaryOperator<T>` interface.
6+
- It is a specialized version of `Function<T, T>`.
7+
8+
Example: Incrementing an integer by 1
9+
10+
import java.util.function.UnaryOperator;
11+
12+
public class UnaryOperatorDemo {
13+
public static void main(String[] args) {
14+
// Define a unary operator that increments its input by 1
15+
UnaryOperator<Integer> increment = x -> x + 1;
16+
17+
// Apply the operator
18+
int result = increment.apply(5);
19+
System.out.println("Increment 5: " + result); // Output: Increment 5: 6
20+
}
21+
}
22+
23+
Explanation:
24+
- UnaryOperator<Integer> increment: Defines a function taking Integer → Integer.
25+
- Lambda expression (x -> x + 1): Adds 1 to the input.
26+
- increment.apply(5): Returns 6.
27+
28+
29+
2. Binary Operator
30+
- A binary operator takes two arguments of the same type and returns a result of the same type.
31+
- In Java, this is represented by the `BinaryOperator<T>` interface.
32+
- It is a specialized version of `BiFunction<T, T, T>`.
33+
34+
Example: Adding two integers
35+
36+
import java.util.function.BinaryOperator;
37+
38+
public class BinaryOperatorDemo {
39+
public static void main(String[] args) {
40+
// Define a binary operator that adds two integers
41+
BinaryOperator<Integer> add = (x, y) -> x + y;
42+
43+
// Apply the operator
44+
int result = add.apply(5, 10);
45+
System.out.println("Add 5 and 10: " + result); // Output: 15
46+
}
47+
}
48+
49+
Explanation:
50+
- BinaryOperator<Integer> add: Takes (Integer, Integer) → Integer.
51+
- Lambda expression ((x, y) -> x + y): Adds the two integers.
52+
- add.apply(5, 10): Returns 15.
53+
54+
55+
3. When to Use
56+
- Use UnaryOperator<T> when:
57+
- You need to transform or modify a single value of type T.
58+
- Example: Incrementing numbers, converting strings to uppercase, squaring a value.
59+
60+
- Use BinaryOperator<T> when:
61+
- You need to combine two values of the same type into one.
62+
- Example: Summing numbers, finding the maximum of two values, concatenating strings.
63+
64+
65+
Summary
66+
- UnaryOperator<T> → One input, same type output. (Function<T, T>)
67+
- BinaryOperator<T> → Two inputs, same type output. (BiFunction<T, T, T>)
68+
- Both are widely used in streams (map, reduce, collect) and functional programming in Java.

0 commit comments

Comments
 (0)