Skip to content

What Are UnaryOperator And BinaryOperator's?

Unary and Binary operators are the other two functional interfaces that most developers are interested in.

1 min read

Table of Contents

There are two other functional interfaces in Java that represent operations that take one or two operands respectively, and produce a result of the same type as the operands.

Other functional interfaces commonly used

These interfaces are used by developers on a day to day, which are:

  1. UnaryOperator
  2. BinaryOperator


UnaryOperator is a functional interface and it extends Function.

UnaryOperator is a functional interface that takes a single operand of a specified type, and returns a result of the same type.

This interface extends the Function interface so the methods of the Function the interface can be used in the implementation of the UnaryOperator interface.

The UnaryOperator interface is defined in the java.util.function package.


public interface UnaryOperator<T> {
    T apply(T t);

For example, you can create a UnaryOperator that doubles an integer value like this:

import java.util.function.UnaryOperator;

public class UnaryOperatorExample {
  public static void main(String[] args) {
    UnaryOperator<Integer> doubleOperator = x -> x * 2;
    int result = doubleOperator.apply(Constants.VALUE); // result will be 10

    System.out.println(result); // 10


BinaryOperator is also a functional interface and it extends BiFunction.

BinaryOperator is a functional interface that takes two operands of a specified type, and returns a result of the same type.


public interface BinaryOperator<T> {
    T apply(T t1, T t2);

For example, you can create a BinaryOperator that adds two integers like this:

import java.util.function.BinaryOperator;

public class BinaryOperatorExample {
  public static void main(String[] args) {
    BinaryOperator<Integer> addOperator = Integer::sum;
    int result =
            Constants.INTEGER_FIRST_VALUE, Constants.INTEGER_SECOND_VALUE); // result will be 5

    System.out.println(result); // 5

Both UnaryOperator and BinaryOperator are useful when you want to pass a function as an argument to another method, or when you want to create a lambda expression that can be reused in multiple places.

Java Streams APIJava

ggorantala Twitter

Gopi has a decade of experience with a deep understanding of Java, Microservices, and React. He worked in India & Europe for startups, the EU government, and tech giants.


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify it. This means when

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class. // your class class Template