![]() |
VOOZH | about |
The IntUnaryOperator Interface is a part of the java.util.function package, which has been introduced since Java 8, to implement functional programming in Java. It represents a function that takes in one argument and operates on it. Both its argument and return type are of the int data type. It is very similar to using an object of type UnaryOperator<Integer>.
The lambda expression assigned to an object of IntUnaryOperator type is used to define its applyAsInt(), which eventually applies the given operation on its argument.
There are different kinds of methods present in the IntUnaryOperator interface, which is defined below:
This method returns an IntUnaryOperator, which takes in one int value and returns it. The returned IntUnaryOperator does not perform any operation on its only value.
Syntax:
static IntUnaryOperator identity()
Example:
12
This method takes in one int value, performs the given operation and returns an int-valued result.
Syntax:
int applyAsInt(int operand)
Example:
24
It returns a composed IntUnaryOperator wherein the parameterized operator will be executed after the first one. If either operation throws an error, it is relayed to the caller of the composed operation.
default IntUnaryOperator andThen(IntUnaryOperator after)
Example 1:
72
Example 2:
Output:
It returns a composed IntUnaryOperator wherein the parameterized operation will be executed first and then the first one. If either operation throws an error, it is relayed to the caller of the composed operation.
Syntax:
default IntUnaryOperator compose(IntUnaryOperator before)
Example 1:
24
Example 2:
Output: