Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
IntUnaryOperator Interface in Java
Next article icon

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
IntUnaryOperator Interface in Java

P

psil123
Improve
Article Tags :
  • Java
  • Java - util package
  • java-basics
  • java-interfaces
  • Java 8
  • Java-Functional Programming
Practice Tags :
  • Java

Similar Reads

    Iterator Interface In Java
    Java Iterator Interface of java collections allows us to access elements of the collection and is used to iterate over the elements in the collection(Map, List or Set). It helps to easily retrieve the elements of a collection and perform operations on each element. Iterator is a universal iterator a
    6 min read
    IntStream map(IntUnaryOperator mapper) in Java
    IntStream map(IntUnaryOperator mapper) returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their p
    2 min read
    IntFunction Interface in Java with Examples
    The IntFunction 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 which takes in an int-valued argument and produces a result of type R. This functional interface takes in only one gener
    1 min read
    Java 8 | IntSupplier Interface with Examples
    The IntSupplier 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 which does not take any argument but produces an int value. The lambda expression assigned to an object of IntSupplier t
    1 min read
    Marker Interface in Java
    In Java, a marker Interface is an empty interface that has no fields or methods. It is used just to mark or tag a class to tell Java or other programs something special about that class. These interfaces do not have any methods inside but act as metadata to provide information about the class. Examp
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences