-1

Lambdas in Java 8 provides a great way to define the problem as a statement and keeps the code concise. But do they change the way in which statements are executed in the JVM?

Lets consider i am writing a lambda which streams a list of objects and filters them based on a condition and returns the filtered values as a list of the same data type. My lambda would basically have a filter and a collector. When this piece of code is sent to the VM, does the compiler replace the lambda functions with traditional if loops and for loops?

I am asking this because the Virtual Machine is still the same and it was originally designed for imperative style of programming. What really changed in Java 8 was the way(FP) in which we define a problem statement. Is my understanding correct?

Beard n Nerd
  • 78
  • 1
  • 9

1 Answers1

2

The compiler doesn't "replace" the lambda functions. The lambdas just become instances of normal interfaces that get passed to normal Java libraries that get run as normal Java code.

The JIT's certainly been optimized somewhat to be smarter about the ability to inline and simplify lambda calls, but there's no magic there.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • I doubt that the JIT knows anything about lambdas. Since they are composed of methods and classes just like any other code, a JIT will attempt to recognize known patterns, regardless of whether the code in question stems from a lambda expression. Applying a potential optimization to the code of lambda expressions only, would be an unnecessary restriction. – Holger Apr 13 '16 at 11:24