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.

Gopi Gorantala
Gopi Gorantala
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

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.

Syntax

@FunctionalInterface
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;
import src.dev.ggorantala.constants.Constants;

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

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.

Syntax:

@FunctionalInterface
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;
import src.dev.ggorantala.constants.Constants;

public class BinaryOperatorExample {
  public static void main(String[] args) {
    BinaryOperator<Integer> addOperator = Integer::sum;
    int result =
        addOperator.apply(
            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

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


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

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.