Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalents to Delphi language features [duplicate]

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)

like image 443
Alex James Avatar asked Sep 14 '26 12:09

Alex James


2 Answers

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

like image 78
mjn Avatar answered Sep 17 '26 02:09

mjn


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.

like image 34
Dalija Prasnikar Avatar answered Sep 17 '26 02:09

Dalija Prasnikar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!