Function
A function is a type of functional interface in Java that receives only a single argument and returns a value after the required processing. There are many versions of Function interfaces because a primitive type can’t imply a general type argument, so we need these versions of function interfaces. Many different versions of the function interfaces are instrumental and are commonly used in primitive types like double, int, long. The different sequences of these primitive types are also used in the argument.
These versions are:
Bi-Function
The Bi-Function is substantially related to a Function. Besides, it takes two arguments, whereas Function accepts one argument.
The prototype and syntax of Bi-Function is given below –
@FunctionalInterface public interface BiFunction<T, U, R> { R apply(T t, U u); ....... }
In the above code of interface, T and U are the inputs, and there is only one output which is R.
Unary Operator and Binary Operator
There are also two other functional interfaces which are named Unary Operator and Binary Operator. They both extend the Function and Bi-Function, respectively. In simple words, Unary Operator extends Function, and Binary Operator extends Bi-Function.
The prototype of the Unary Operator and Binary Operator is mentioned below :
i. Unary Operator
@FunctionalInterface public interface UnaryOperator<T> extends Function<T, U> { ……... }
ii. Binary Operator
@FunctionalInterface public interface BinaryOperator<T> extends BiFunction<T, U, R> { ……... }
We can understand front the above example that the Unary Operator accepts only one argument and returns a single argument only. Still, in Unary Operator both the input and output values must be identical and of the same type.
On the other way, Binary Operator takes two values and returns one value comparable to Bi- Function but similar to a Unary Operator, the input and output value types must be identical and of the same type.
Functional Interfaces in Java
Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some of the primitive data types and primitive methods for integrity and simplicity. There are no solely functions present in a programming language called Java. Functions in the Java programming language are part of a class, and if someone wants to use them, they have to use the class or object of the class to call any function.