Are Kotlin inline functions less expensive than Java anonymous classes?

Viewed 1499

Heads up: I'm writing some of this from memory so I may have some of the concepts incorrect.


Java has the ability to write an anonymous function. This is useful when you have a listener interface for some kind of event. As an example:

button.setOnClickListener(new View.OnClickListener(View v) {
    @Override
    public void onClick(View v) {
        // handle the action here
    }
});

The anonymous listener will be compiled as a class that is called something like OnClickListener$1.class. This is an underlying design decision of the Java language. Everything is an object, even anonymous functions.

This becomes an issue when you want to write a more functionally driven code base. The large amount of anonymous classes creates a large class count, which can be a problem on constrained platforms such as Android.

In Kotlin functions are much more first class from a source code point of view. My question is, does Kotlin compile these functions down to byte code more efficiently than Java does with anonymous classes or will I run into the same issues as the large class count in Java?

Thanks,

2 Answers
Related