3

I normally code in Delphi but am having to play catch-up in Java fast for one particular project. I'm having a problem identifying Java equivalents to a number of Delphi language features because presumably different terminology is used to refer to them.

I see from the Java language specification that it supports lambda expressions so I imagine I'll be able to find examples somewhere showing the Java equivalent to anonymous methods.

My question here is does Java have equivalents to the following Delphi types and, if so, what are they named or what are the equivalent Java constructs:

  1. Tradition procedural,functional types, as in type MyProc = procedure(I : Integer)

  2. procedure of object and function of object

?

(I hope both of these are close enough to be asked in a single question)

mjn
  • 36,362
  • 28
  • 176
  • 378
Alex James
  • 696
  • 5
  • 13
  • Have you tried to find the answer in the [Java Language Specification](https://docs.oracle.com/javase/specs/)? – mjn Mar 20 '16 at 18:09
  • 1
    I'm not familiar with Delphi, but I suspect the answer is no as Java does not have function types. Every lambda expression has to target a specific functional interface that can be inferred from the context. – Paul Boddington Mar 20 '16 at 18:18
  • @mjn: Of course, that's where I found the reference to Lambdas. But the v.8 spec is 788 pages and without knowing the relevant grammatical constructs / terminology, I don't really know what to look for. That's why I asked ... – Alex James Mar 20 '16 at 18:19
  • @PaulBoddington: That might explain why I can't find anything so far as my 1. is concerned;=). Google found smth that may be relevant to my 2. : https://books.google.co.uk/books?id=9CL446IzhuAC&pg=PA226&lpg=PA226&dq=java+%22procedure+of+object%22&source=bl&ots=qnNSJwEte-&sig=bhISLCr0YKYvxSRIYbdlkv7S_js&hl=en&sa=X&ved=0ahUKEwjQ5fLk8M_LAhWJOBoKHQwUD8MQ6AEIIzAB#v=onepage&q=java%20%22procedure%20of%20object%22&f=false, which seems to suggest I need to look at java.reflect.Method ... – Alex James Mar 20 '16 at 18:24
  • 2
    Whoever downvoted: How is it that people can regularly ask zero-researched, trivial, Delphi 101 questions and not get -1s, but asking a valid, researched q about language equivalents gets one? – Alex James Mar 20 '16 at 18:29
  • Another dupe: http://stackoverflow.com/questions/44912/java-delegates – David Heffernan Mar 20 '16 at 22:38

2 Answers2

4
  1. In Java, methods are never stand-alone, they are always bound to interfaces/classes. So there is nothing to create type references for.

  2. Check for example Java SE 8: Lambda Quick Start.

It uses a Lambda expression and assigns it to a variable allPilots ...

Predicate<Person> allPilots = p -> p.getAge() >= 23 && p.getAge() <= 65;

... and uses it to invoke the method from a different place:

System.out.println("\n=== Mail all Pilots ===");
robo.mailContacts(pl, allPilots);
...
public void mailContacts(List<Person> pl, Predicate<Person> pred) {
    for (Person p : pl) {
        if (pred.test(p)) {
            roboEmail(p);
        }
    }
}

where the Predicate interface is defined as

public interface Predicate<T> {
    public boolean test(T t);
}

This is functionally close to a function of object in Delphi (because the interface has a return type). For a method of object, the functional interface simply has no return type (void).

mjn
  • 36,362
  • 28
  • 176
  • 378
3

Java does not have equivalent of Delphi Procedural Types that would allow you to treat procedures and functions as values that can be assigned to variables or passed to other procedures and functions.

Closest functional match would be Java Anonymous Class that allows you to declare and instantiate class at same time.

Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159