What is the Identity() method of the IntUnaryOperator in Java?

Overview

In Java, identity is a static method of the IntUnaryOperator interface, used to return an IntUnaryOperator that always returns the input argument.

Syntax

static IntUnaryOperator identity()

Parameters

The method takes no parameters.

Return value

The method returns an IntUnaryOperator that always returns its input argument.

Example

import java.util.function.IntUnaryOperator;
public class Main {
public static void main(String[] args){
IntUnaryOperator identity = IntUnaryOperator.identity();
int arg = 343;
int arg1= 12;
System.out.printf("(%s == identity.applyAsInt(%s)) = %s \n", arg1, arg1, (arg1 == identity.applyAsInt(arg1)));
System.out.printf("(%s == identity.applyAsInt(%s)) = %s \n", arg1, arg, (arg1 == identity.applyAsInt(arg)));
System.out.printf("(%s == identity.applyAsInt(%s)) = %s \n", arg, arg1, (arg == identity.applyAsInt(arg1)));
System.out.printf("(%s == identity.applyAsInt(%s)) = %s", arg, arg, (arg == identity.applyAsInt(arg)));
}
}

Explanation

  • Line 1: We import the IntUnaryOperator interface.
  • Line 6: We use the identity() method to define an identity function.
  • Lines 7–8: We define two integer values called arg and arg1.
  • Lines 10–13: We use applyAsInt() to check whether the integer values arg, arg1, and the value returned are equal.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources