Open In App

IntUnaryOperator Interface in Java

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Interface

There 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()

  • Parameters: This method does not take any parameters.
  • Returns: It returns an IntUnaryOperator, which takes in one value and returns it.

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)); 
	} 
} 

Output
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)

  • Parameters: This method takes in one int valued parameter.
  • Returns:: It returns an int valued result.

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)); 
	} 
} 

Output
24


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)

  • Parameters: This method accepts a parameter after which is the operation to be applied after the current one.
  • Return Value: This method returns a composed IntUnaryOperator that applies the current operation first and then the after operation.
  • Exception: This method throws NullPointerException if the after operation is null.

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)); 
	} 
} 

Output
72


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:

NullPointerException


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)

  • Parameters: This method accepts a parameter before which is the operation to be applied first and then the current one
  • Return Value: This method returns a composed IntUnaryOperator that applies the current operator after the parameterized operator
  • Exception: This method throws NullPointerException if the before operation is null.

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)); 
	} 
} 

Output
24


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:

ExceptionOutputNullPointer


Next Article

Similar Reads