Identity method of the DoubleUnaryOperator interface in Java

identity is a static method of the DoubleUnaryOperator interface that returns the same input argument as DoubleUnaryOperator.

Syntax

static DoubleUnaryOperator identity()

Parameters

This method has no parameters.

Return value

The method returns a unary operator that always returns its input argument.

Code

import java.util.function.DoubleUnaryOperator;
public class Main {
public static void main(String[] args){
DoubleUnaryOperator identity = DoubleUnaryOperator.identity();
double arg = 1.5;
System.out.printf("(%s == identity.applyAsDouble(%s)) = %s", arg, arg, (arg == identity.applyAsDouble(arg)));
}
}

Explanation

  • Line 1: We import the DoubleUnaryOperator interface.
  • Line 6: We define an identity function using the identity() method.
  • Line 7: We define a double called arg.
  • Line 8: We check whether arg and the value returned are equal. We do this by using the applyAsDouble() method, passing arg as an argument.

Free Resources