Is it possible to cast an IntBinaryOperator to a String in java? What I have here is a simplification of the program I want to run
import java.util.function.IntBinaryOperator;
public class Showcase {
public static void main(String args[]){
IntBinaryOperator op = (a,b) -> a + b;
System.out.println(op.toString());
}
}
You can implement IntBinaryOperator
as a class rather than a lambda, and override toString()
:
IntBinaryOperator op = new IntBinaryOperator() {
@Override public int applyAsInt(int a, int b) { return a + b; }
@Override public String toString() { return "(a, b) -> a + b"; }
};
System.out.println(op);
but you have to implement both methods "manually", there's no easy way to get that toString()
automatically from the applyAsInt
method, nor to enforce that the toString()
method is an accurate representation of the logic in applyAsInt
.