Sobes.tech
Junior — Middle

Is it possible to pass a function or method as a parameter to another method or function?

sobes.tech AI

Answer from AI

In Java, you can pass functions or methods as parameters using functional interfaces and lambda expressions (starting from Java 8). For example, the Function<T, R> interface allows passing a function that takes a parameter of type T and returns R.

Example:

import java.util.function.Function;

public class Example {
    public static void execute(Function<String, Integer> func) {
        Integer result = func.apply("123");
        System.out.println(result);
    }

    public static void main(String[] args) {
        execute(s -> Integer.parseInt(s));
    }
}

Thus, methods and functions can be passed as parameters through functional interfaces.

Is it possible to pass a function or method as a… - sobes.tech