Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda vs anonymous inner class performance: reducing the load on the ClassLoader?

I would like to know how big of a benefit lambdas have in Java 8. I agree that it might be more readable sometimes to use lambdas, but does it have really such of a big impact on the performance side? Or is it mainly focused as syntactic sugar? I prefer anonymous inner classes sometimes; do I really lose many benefits when I don't use lambda all the time?

The only ?big? performance gain, it seems to me, is that we don't actually create classes that the class loader has to load at the start of the program -- for example creating many many threads:

Thread t = new Thread(new Runnable() {
   public.....
});

creates classes like Sample$1.class.

Other than that, is there any performance or other hidden gain besides readability or maintainability etc. of the code? Hidden somewhere in JVM? I've seen questions similiar to this but most of them were focused on visual side; I'm not interested in that. The question is out of curiosity after watching Java 8 Lambdas Hacking with Venkat Subramaniam.

like image 765
Tomas Bisciak Avatar asked Jul 15 '14 17:07

Tomas Bisciak


People also ask

Which is better lambda expression or anonymous inner class?

Lambda expression can be used where a class implements a functional interface to reduce the complexity of the code. An inner anonymous class is more powerful as we can use many methods as we want, whereas lambda expression can only be used where an interface has only a single abstract method.

What is the difference between lambda vs anonymous class?

Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time. A lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name.

Does lambda expression improve performance?

Oracle claims that use of lambda expressions also improve the collection libraries making it easier to iterate through, filter, and extract data from a collection. In addition, new concurrency features improve performance in multicore environments.

Why would you use an anonymous class rather than an inner class?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

What is the difference between anonymous Lambda and anonymous inner class?

Lambda is called anonymous function ie. method without name. However, an Anonymous Inner class is called a class without name. However, a Lambda Expression occupies permanent memory of JVM. Anonymous Inner class occupies memory whenever object is created on demand. For Lambda Expression at the time of compilation, no .class file fill be generated.

What is the difference between “this” and “Lambda”?

Inside Anonymous inner class, “this” always refers to current anonymous inner class object but not to outer object. Inside Lambda Expression, “this” always refers to current outer class object that is, enclosing class object.

Is it possible to instantiate a lambda expression?

Lambda Expression can’t be instantiated. Inside Anonymous inner class, “this” always refers to current anonymous inner class object but not to outer object. Inside Lambda Expression, “this” always refers to current outer class object that is, enclosing class object.

What is an anonymous class in Java 8?

Java 8 Object Oriented Programming Programming Anonymous class is an inner class without a name, which means that we can declare and instantiate class at the same time. A lambda expression is a short form for writing an anonymous class. By using a lambda expression, we can declare methods without any name.


1 Answers

Oracle has a presentation covering some of the performance differences. It appears that there are quite a few factors that impact performance of lambdas vs. anonymous classes.

http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf

like image 168
Brett Okken Avatar answered Nov 15 '22 22:11

Brett Okken