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)

6
  • Have you tried to find the answer in the Java Language Specification? Commented Mar 20, 2016 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. Commented Mar 20, 2016 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 ... Commented Mar 20, 2016 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. : books.google.co.uk/…, which seems to suggest I need to look at java.reflect.Method ... Commented Mar 20, 2016 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? Commented Mar 20, 2016 at 18:29

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.