Last Updated : 11 Jul, 2025
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.
Different Methods of The IntUnaryOperator InterfaceThere are different kinds of methods present in the IntUnaryOperator interface, which is defined below:
1. identity()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:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = IntUnaryOperator.identity();
System.out.println(op.applyAsInt(12));
}
}
2. applyAsInt()
This method takes in one int value, performs the given operation and returns an int-valued result.
Syntax:
int applyAsInt(int operand)
Example:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> 2 * a;
System.out.println(op.applyAsInt(12));
}
}
3. andThen()
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:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> 2 * a;
op = op.andThen(a -> 3 * a);
System.out.println(op.applyAsInt(12));
}
}
Example 2:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> 2 * a;
op = op.andThen(null);
System.out.println(op.applyAsInt(12));
}
}
Output:
4. compose()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:
Java
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> a / 3;
op = op.compose(a -> a * 6);
System.out.println(op.applyAsInt(12));
}
}
Example 2:
C++
import java.util.function.IntUnaryOperator;
public class Geeks
{
public static void main(String args[])
{
IntUnaryOperator op = a -> a / 3;
op = op.compose(null);
System.out.println(op.applyAsInt(12));
}
}
Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4