Classes defined by using lambda in Java [duplicate]

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;
        }
    }

}

4

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;
        }
    }

}

How many classes are defined by this code? Two. AI and Main. Lambdas aren’t classes or anonymous classes. They’re implemented as something much more complex.

How many objects are allocated by this code? It’s not specified. The machinery involved in creating lambdas is likely to allocate a very large number of objects, none of which are obvious. The class that’s likely to be created for the lambda at runtime should get reused for each loop, though. It’s not likely that the lambda object itself is reused, since it captures j, but it’s also legal for the compiler to eliminate the for loop entirely.

1

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.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật