Hi I am trying to understand the advantages of using Lambda expression, where I heard usage of the invokeDynamic byteCode by JVM to execute lambda expression will improve the performance compared to XXXX(Sorry I don't know, it might be an Anonymous inner class).
My question is Why Lambda expressions need invokeDynamic byteCode.
For example:
public class LambdaTest {
public static void main(String[] args) {
MathOperation addition = (int a, int b) -> a + b;
addition.operation(1, 2);
}
private static int operate(int a, int b, MathOperation operation){
return operation.operation(a, b);
}
interface MathOperation {
int operation(int a, int b);
}
}
where the lambda expression (int a, int b) -> a + b; can be simply desugared into a static method like
private static int lambda$1(int a, int b) {
return a + b;
}
which eventually can be invoked using invokstatic byteCode right?
More questions: What is the dynamicity lambda expression trying to achieve, when all the argument type, returns type are defined during the compile time itself.
For example: in the lambda expression (int a, int b) -> a + b; everything(argument type, return type) got defined during the compile time itself right?
The method holding the body of the lambda expression is not invoked via invokedynamic. The implementation of the functional interface is invoked via invokeinterface, just like any other interface method. The caller even doesn’t know whether an object implementing an interface has been generated for a lambda expression.
How this generated implementation of the interface invokes the synthetic method, is JRE specific, but usually it happens via an ordinary invocation (invokestatic, invokevirtual or invokespecial). Only the access rights follow different rules, i.e. the method can be invoked by the generated class despite being private.
The invokedynamic instruction is used for the instantiation of the interface implementation, so it allows arbitrary, unknown implementation classes, including classes, which do not exist at compile-time, but are generated at runtime. It also opens the possibility to return an existing instance instead of creating a new one, which is impossible for ordinary instance creation code.
Related
How to implement this function below
public T myFunction(Function<T> func) //T is the return type
{
//...some code
return func();
}
And I can use the function like below, and don't need to declare parameters a, b, c in myFunction
myType result = myFunction(() -> doSomething(a, b , c))
The right functional interface to use then is Supplier<T>. It doesn't have an "input" type parameter:
public <T> T myFunction(Supplier<T> func) {
// myFunction logic
return func.get();
}
And the invocation will be just as you have it, assuming doSomething() has myType as return type.
Unfortunately, Java is not a "Functional" language. So when you define a method like myFunction(Function<T> func) you specified, myFunction requires an Object that extends the Interface Function. Function in java declares a single method R apply(T t) in this case, Function takes a single argument. There are additional Interfaces that are part of the Java SDK such as BiFunction which has an apply method that takes two arguments. If you need a method that will take a "Function" requiring three parameters you will have to declare an Interface with those specifics. Or you could use something like. Apache Commons TriFunction
In the following program, I have one single method in class A overloaded 3 times and then in subclass B, all 3 overloaded methods are overridden.
obj3 is an object with reference type A(superclass) and object type B(subclass) and it calls the method from B on execution, which is expected behavior.
Since overloading and overriding both exist in this code, does that mean that it performed static binding at compile time (to the matching method in class A) and then dynamic binding at run time (to method in class B). Can they both occur together?
My assumption is that this is a classic case of dynamic binding as I believed "binding" is meant to be a permanent action, but a peer suggests that it is both together(static first, then dynamic).
class A{
public void method(Integer n){
System.out.println("Integer: "+n);
}
public void method(String s){
System.out.println("String: "+s);
}
public void method(String s, Integer n){
System.out.println("String: "+s+" Integer: "+n);
}
}
class B extends A{
public void method(Integer n){
System.out.println("Integer(from B): "+n);
}
public void method(String s){
System.out.println("String(from B): "+s);
}
public void method(String s, Integer n){
System.out.println("String(from B): "+s+" Integer(from B): "+n);
}
}
public class Test{
public static void main(String[] args){
A obj1 = new A();
B obj2 = new B();
A obj3 = new B();
System.out.println("Integer form of method");
// Integer form of method
System.out.println("Ref A Obj A");
// Ref A Obj A
obj1.method(1);
// Integer: 1
System.out.println("Ref B Obj B");
// Ref B Obj B
obj2.method(2);
// Integer(from B): 2
System.out.println("Ref A Obj B");
// Ref A Obj B
obj3.method(3);
// Integer(from B): 3
}
}
Since overloading and overriding both exist in this code, does that mean that it performed static binding at compile time (to the matching method in class A) and then dynamic binding at run time (to method in class B)
Right. The compiler chose the matching signature, and this is static, based on the type of the variable (A in this case).
At runtime, Java finds the implementation of the signature selected by the compiler. This is dynamic, based on the runtime class of obj3 (B, in this case).
You right. Compiler is statically choose between overloads in class A and put that information into .class file in form of method FQN.
Then runtime dynamically choose between implementation of that method.
One more attempt to make it clear:
Overloading
Overloading means that a single class has multiple methods with different parameter types (aka. signatures), and you just happened to give them the same name. Your program will work the very same if you change to individual names for the methods, e.g. methodI(Integer n), methodS(String s), and methodSI(String s, Integer n).
Or, you can imagine the compiler to internally always append such a types list to the method name.
Overloading is resolved by the compiler, based on the compile-time types of the parameter expressions.
E.g. if you write
Object par = "Test";
a.method(par);
you get a compiler error. Even though we all see it's a String that you are passing into the method, the compiler only sees an Object, and finds no matching method. Only if you were to introduce an additional method(Object o), the compiler would choose that one. And runtime would call that method and not the String version!
Overriding
Overriding means that at runtime the JVM calls the method implementation depending on the runtime class of the "object before the dot".
And in this case, "method" is to be read as the overloaded method version that the compiler found to match the parameter list. So, runtime already knows whether methodI(), methodS(), or methodSI() is meant, and only decides from which class to take the implementation.
Personal opinion
Allowing multiple methods to share the same name, but differ in parameter lists (aka overloading), produces too much confusion for its benefit.
I'm learning how to use lambda expressions now, and I've seen some tutorials with a simple example:
(int x) -> x + 5;
But my compiler is showing this error:
Syntax error, insert "AssignmentOperator Expression" to complete Expression
Am I forgetting something?
Lambda expressions always have to be assigned to a reference type of Functional Interafces (also called single abstract method interfaces). Infact, they provide shortcut to the verbose anonymous class (with single method) implementations.
So, in simple words, Lambda expression = abstract method implementation (of the functional interface).
For example, your expression can be assigned to the below Functional Interface:
public interface MyInterface {//define Functional Interafce (SAM)
public int someMethod(int a);
}
public class Test {
public static void main(String[] args) {
MyInterface myInterface = (int a) -> a +5;//assign the expression to SAM
int output = myInterface.someMethod(20)); //returns 25
}
}
Lambdas are expressions that cannot be used as statements. From JLS8 §15.27:
It is a compile-time error if a lambda expression occurs in a program in someplace other than an assignment context (§5.2), an invocation context (§5.3), or a casting context (§5.5).
Consider this example:
// functional interface
interface Operator
{
int apply(int a, int b);
}
// method that expects instance of the interface
int calculate(int a, int b, Operator op)
{
return op.apply(a, b);
}
// lambda expression
Operator plus = (a, b) -> a + b;
// method call
calculate(40, 2, plus);
The issue is, as pointed out above, you are not doing anything with the lambda. This means that:
the compiler does not know which functional interface (e.g. java.util.function.Function) to infer as the type for your lambda.
Your line of code is "not a statement" (another error message often emitted by the compiler). This is similar to something like this:
"Hello";
Which is not valid Java.
A lambda expression cannot stand alone in Java, it need to be associated to a functional interface.
public interface myinterface
{
int mymethod(int a,int b);
}
public static void main(String[] args)
{
myinterface my = ( a,b ) -> {
int mul = a*4;
int add = a+b;
return add; };
}
Note: This is the first and last time you will see implemention of interface without keyword "Implements".
Play Around: try adding a new dummy method to your interface myinterface and you will see that your code will fail to compile, thus indicating that reference has to be only made from Functional interface not from general interfaces.
public interface myinterface
{
int mymethod(int a,int b);
int newmethod(String j);
}
public static void main(String[] args) {
myinterface my = ( a,b ) -> {
int mul = a*4;
int add = a+b;
return add;
};
Compilation Error: The target type of this expression must be a
functional interface
There are several similar questions on SO about method reference to local class constructor, but I'd like to clarify slightly other thing. Consider following piece of code:
static Callable gen(int i) {
class X {
int x = i;
public String toString() { return "" + x; }
}
return X::new;
}
...
System.out.println(gen(0).call());
System.out.println(gen(1).call());
Obviously this will printout
0
1
It turns out, that X class has constructor of the form ...$X(int) (you can find it via X.class.getDeclaredConstructors()).
But what is interesting here, is that returned lambdas (or method references) aren't simple reference to constructor ...$X(int) like, for example, Integer::new. They internally invoke this constructor ...$X(int) with predefined argument (0 or 1).
So, I'm not sure, but looks like this kind of method reference is not precisely described in JLS. And there is not other way except this case for local classes, to produce such kind of lambdas (with predefined constructor arguments). Who can help clarify this?
To be precise:
where is in JLS such kind of method reference described?
is any other way to create such method reference to arbitrary class constructor with predefined arguments?
You are focusing too much on irrelevant low level details. On the byte code level, there might be a constructor accepting an int parameter, but on the language level, you didn’t specify an explicit constructor, hence, there will be a default constructor without any arguments, as with any other class.
This should become clear when you write the pre-Java 8 code:
static Callable<Object> gen(int i) {
class X {
int x = i;
public String toString() { return "" + x; }
}
X x=new X();
…
You instantiate X by its default constructor, not taking any arguments. Your local class captures the value of i, but how it does so on the low level, i.e. that X’ constructor has a synthetic int parameter and the new expression will pass the value of i to it, is an implementation detail.
You can even add an explicit constructor as
X() {}
without changing anything.
Obviously, you can also write the expression new X() inside a lambda expression here, as expressions don’t change their semantic when being placed inside a lambda expression:
return () -> new X();
or use it’s short-hand form, the method reference
return X::new;
There is nothing special about it, the behavior is understandable even without referring to the specification, if you forget about the distracting low level details. X may capture as many local variables as you like, the constructor’s number of parameters doesn’t change (on the language level).
This behaviour is defined in the JLS section §15.13.3:
If the form is ClassType :: [TypeArguments] new, the body of the invocation method has the effect of a class instance creation expression of the form new [TypeArguments] ClassType(A1, ..., An), where the arguments A1, ..., An are the formal parameters of the invocation method, and where:
The enclosing instance for the new object, if any, is derived from the site of the method reference expression, as specified in §15.9.2.
The constructor to invoke is the constructor that corresponds to the compile-time declaration of the method reference (§15.13.1).
Although this talks about enclosing instances, captured variables and parameters are not mentioned in §15.13.3.
As for your second question, you need to manually capture and change the parameter:
static Callable gen(int i) {
final int i1 = someCondition() ? i : 42;
class X {
int x = i1; // <-
public String toString() { return "" + x; }
}
return X::new;
}
I have implemented the following code in order to understand the difference between static and dynamic binding:
class A {
int met(A a) {
return 0;
}
int met(B b) {
return 1;
}
int met(C c) {
return 2;
}
}
class B extends A {
int met(A a) {
return 3;
}
int met(B b) {
return 4;
}
int met(C c) {
return 5;
}
}
class C extends B {
int f() {
return ((A)this).met((A)this);
}
}
public class Test {
public static void main(String[] args) {
C x = new C();
System.out.println(x.f());
}
}
The result I get is 3, but I don't understand why, since the first cast is made to A.
So, let's look at the call:
((A)this).met((A)this);
That's equivalent to:
A target = this;
A argument = this;
target.met(argument);
So, for the last line, the compiler looks up the signature based on the compile-time types involved - it will look in A (and superclasses) for a method called met which has a parameter that is compatible with A (the compile-time type of the argument). Overload resolution finds that the answer is:
int met(A a)
That's determined the signature at compile-time. However, the implementation of that method is determined at execution time, based on the execution-time target of the method call. That type is C here - because this is a reference to an instance of C. (The f method is called on an instance of C.)
Now C doesn't override int met(A a), but B (its superclass) does - so that's the implementation that's used. It doesn't matter that B also overrides met(B b) and met(C c) because the compiler has already determined that it's the met(A a) method which is being called.
This is two questions for the price of one. (1) Why do we get the implementation from class B, and (2) why do we get the version of the method with the parameter of type A.
For question (1), the thing to remember is that the class of an object doesn't change when you cast it, or assign it to a variable whose type is something different. So in your example, the class of this is always C, because that's what you created. Class C inherits its version of met(A a) from class B because it doesn't have its own version, and because class B has overridden the version in class A. This is what polymorphism is all about - the version of the method depends on the class of the object that you call it on, not on the type of the expression that you use to call it.
For question (2), it's a wee quirk of Java that method signatures are evaluated at compile time. So the compiler sees that you're passing an expression of type A into your method, so it chooses the signature met(A a). Because this decision is made at compile time, the actual class of the argument doesn't make any difference - the compiler has already chosen the method, based on the type of the expression. In other words, Java doesn't give you polymorphism of method parameters.