linhao linhao - 1 year ago 21
Java Question

why lambda expression doesn't write `.class` but inner class do

public static void main(String[] args) {

Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Integer.compare(o1.length(), o2.length());
}
};

Comparator<String> comparator1 = (String a, String b) -> {
return Integer.compare(a.length(), b.length());
};


Comparator<String> comparator2 = Comparator.comparing(String::length);

}


class file:
LambdaAndAnonymousInner$1.class and
LambdaAndAnonymousInner.class

the LambdaAndAnonymousInner$1.class is
comparator


comparator1
and
comparator2
doesn't write
.class
file

I wonder why it happened

Answer Source

This is because lambdas are invoked dynamically.

Refer to this: http://wiki.jvmlangsummit.com/images/1/1e/2011_Goetz_Lambda.pdf

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download