Equivalent to C# delegate in Java

Hello there.

As a trained C# dev I learnt about delegates and the predefined ones such as Func<>. I was wondering if there is a way to reproduce such behaviour with Java.

I did write a markdown file with further details on GitLab.

It is likely what I try to achieve does not exist in Java and may not be replicable for now. So it is fine if no one actually answers? But I’m just dropping a message in a bottle someone could maybe answer.

Kind Regards.

Sounds like what you are looking for is mostly called lambdas, method references and functional interfaces in Java. Most of that stuff has been available since Java 8 and has gotten a little nicer with time. I’ll try to find you a good reference but if I cannot find one I can certainly give you a demonstration.

1 Like

Using the code in your GitLab, it would look like this:

public static void main(String[] args) {
  for (ThreePeople myFunc : 
       List.of(
         FuncEquivalentDemo::sumAges,
         FuncEquivalentDemo::averageAges)) {
    System.out.println(myFunc.doWithAges(person1, person2, person3));
  }
}

You do have to call the doWithAges method on the object, but that is how it works. You can call a function like askFunc with either a method reference or a lambda:

askFunc(FuncEquivalentDemo::sumAges);
askFunc(FuncEquivalentDemo::averageAges);
askFunc((p1, p2, p3) -> -1);

Since everything in Java is an object, there is no way to get this exact syntax though:

var myFunc = ...;
myFunc(person1, person2, person3);

You have to call the method for the interface in order to run the code.

1 Like

This is about the best description I can find describing functional interfaces:

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html#package.description

1 Like

Hi.

Thank you very much for the quick answer.
I learnt about references and I tried a thing or two but I assumed that because I was in the type where the sumAges() and averageAges() were, I did not need to prefix with type name.

C# was inspired by Java so everything in C# is Object too.
I knew I could not get a perfectly identical solution but that’s close enough to me. Simply in C# you can prepare a delegate and call it when needed while the askFunc(FuncEquivalentDemo::sumAges); statement call it right on. But that’s not a big deal. Your solution works like a charm.
I will update my notes and give you credit (Just 1st letter on name unless you’re OK I put it fully) for the help. :slight_smile:

Like we say in France “Rendre à César ce qui appartient à César” (i.e. Give back to Caesar what belong to Caesar).

Best Regards.

EDIT:

Got through the ref you provided.
Tried something inspired from their Assignation context example and it is fair enough:

public static void furtherDemo() {
//	    Predicate<String> isEmpty = String::isEmpty;
//	    System.out.println(isEmpty.test("Hello"));
//	    System.out.println(isEmpty.negate().test("World"));

	ThreePeople tp = FuncEquivalentDemo::sumAges;

	System.out.println(tp
		.doWithAges(new Person("Jake", 57), 
			new Person("Bill", 25), 
			new Person("Laura", 18)));
}

Output

100

Thanks again.