Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Lambdas : How it works in JVM & is it OOP? [closed]

For example in the case of an anonymous inner class, an (anonymous) object reference is passed and methods of that object are executed.

Lambdas are code blocks which will be executed when needed.

What happens in the JVM when lambdas are encountered? Where does the JVM store the code blocks related to lambdas (Heap : Young, Old or Permanent Generation)?

I tried searching, and I got the syntax for using lambdas but was not able to understand what is happening inside JVM, as in JAVA everything is object-based.

  1. So in context of OOP how do lambdas work?

  2. Do lambdas violate OOP concepts?

  3. Is Lambda good for the garbage collector as no objects are created hence no worry about memory issues and clearing memory?

like image 428
Satty Avatar asked Mar 19 '15 11:03

Satty


People also ask

How does lambda work in JVM?

Lambda expression is compiled using invokedynamic bytecode. Lambda implementation is stored in the same class file as a special private method. Whether an object is created to invoke lambda depends on the situation. In the trivial cases lambda gets translated to a constant method handle.

How does lambda expression work internally?

A lambda works like this: Generates invokedynamic call site and uses a lambdafactory to return the functional implementation. Lambda converted to a method to be invoked by invokedynamic. The method is stored in a class as a private static method.

Are lambdas objects in Java?

Yes, any lambda expression is an object in Java. It is an instance of a functional interface. We have assigned a lambda expression to any variable and pass it like any other object.


2 Answers

I wouldn't waste my time thinking whether the lambda expressions are a violation of OO principles. Its goal is to increase the power of a language and not to write an OO code, I don't see how lambdas can violate encapsulation, inheritance or polymorphism.

This article explains how Java handles lambda expressions:

What’s interesting about Lambda expressions is that from the JVM’s perspective they’re completely invisible. It has no notion of what an anonymous function or a Lambda expression is. It only knows bytecode which is a strict OO specification. It’s up to the makers of the language and its compiler to work within these constraints to create newer, more advanced language elements.

Considering the following code:

List names = Arrays.asList("1", "2", "3"); Stream lengths = names.stream().map(name -> name.length()); 

... It begins quite simply by loading the names var and invokes its .stream() method, but then it does something quite elegant. Instead of creating a new object that will wrap the Lambda function, it uses the new invokeDynamic instruction which was added in Java 7 to dynamically link this call site to the actual Lambda function.

aload_1 //load the names var  // call its stream() func invokeinterface java/util/List.stream:()Ljava/util/stream/Stream;  //invokeDynamic magic! invokedynamic #0:apply:()Ljava/util/function/Function;  //call the map() func invokeinterface java/util/stream/Stream.map: (Ljava/util/function/Function;)Ljava/util/stream/Stream; 

InvokeDynamic is an instruction that was added in Java 7 to make the JVM less strict, and allows dynamic languages to bind symbols at run-time, vs. doing all the linkage statically when the code is compiled by the JVM.

The Lambda code

aload_0 invokevirtual java/lang/String.length:() invokestatic java/lang/Integer.valueOf:(I)Ljava/lang/Integer; areturn 
like image 181
Maroun Avatar answered Oct 08 '22 18:10

Maroun


Lambda expressions don't get translated into anonymous inner classes, they use invoke dynamic that was introduced in Java 7 to execute functional methods. Check this out.

Do they violate OOP? I don't think that you should care. Lambdas make your code less verbose, easier to understand, and "easier" to parallelise. And thats what you should care about.

From Brain Goetz comment:

We don't get paid to write object-oriented programs or functional programs, we get paid to write working programs.

like image 29
Sleiman Jneidi Avatar answered Oct 08 '22 18:10

Sleiman Jneidi