javaooplambdajvm

Classes defined by using lambda in Java


I had a test on OOP in Java and they asked how many classes are defined by the code below.

The question is about the lambda expression in this code. The lambda expression initiates an anonymous class, but here the expression is in a for loop that iterates 3 times. Does this create 3 classes or just one class?

If it matters, we are working with Java 17.

I've tried to ask Chat GPT but it is unreliable and I've tried to search for an answer, but I can't find a complete and specific answer online.

public interface AI {
     int f();
}

public class Main {

    public static void main(String[] args) {
        AI ai;
        for(int i = 0; i < 3; i++) {
            int j = i;
            ai = () -> j + 2;
        }
    }

}

Solution

  • Your line of reasoning is highly confused.

    Given:

    class Dog {
      String name;
    }
    
    void test() {
      Dog a = new Dog();
      Dog b = new Dog();
      Dog c = new Dog();
      Dog d = new Dog();
    }
    

    surely you wouldn't say that '4 classes are defined here, because 4 classes are instantiated'. And yet you are applying that logic to your lambdas here. That is, obviously, wrong.

    lambda expressions initiates an anonymous class

    This is simply false. It's a way to explain lambdas, and a particularly useful way about 12 years ago when lambdas were introduced (lambdas weren't part of java until java v1.8 - decades of java preceded them!), as, until then, solving the problems that lambdas can solve for you required anonymous inner classes.

    It does not mean, and never has meant, that lambdas are just syntax sugar for inner classes. This is literally the case: A lambda simply does not result in a class. How the compiler ends up making a class file that does what you need it to is up to the compiler, but at time of writing, the JDK generally generates a method that contains the 'body' of the lambda and just invokes it if that's all you are doing with the lambda, or makes a methodhandle to refer to it if it needs to be passed around.

    Thus, reading 'how many classes are defined' in some sort of 'at the JVM level' results in the following answer, and this is the only correct answer! (I can imagine a teacher would disagree. That'd be because they are wrong! Feel free to send them to this answer):

    That is not answerable; the Java Lang Spec does not dictate any particular implementation. At least one (Main). AI defines an interface which isn't a class, but, at the JVM level, 'types' is the better word, and certainly at least 2 types are being set up here. Maybe a 3rd - up to the compiler.

    If the question is about the java side of things, the answer is much simpler:

    1 class is being defined (Main), and 1 interface (AI). Period. Classes are defined with class and with the anonymous inner class literal. lambdas are neither of those. The spec literally spells this out. The spec's pages about lambdas don't mention 'this defines a class' anywhere in them.