Java Magazine, Nov/Dec 2017
Understanding Java Method Invocation with Invokedynamic The instruction added in Java 7 makes it possible to resolve method calls dynamically at runtime BEN EVANS ORACLE COM JAVAMAGAZINE NOVEMBER DECEMBER 2017 67 inside the jvm In the first part of this two part series I discussed four of Javas five method invocation opcodes These are the bytecode representations of the standard forms of method invocation used in Java 8 and Java 9 This raises the question of how the fifth opcode invokedynamic enters the picture The short answer is that as of Java 9 there is no direct support for invokedynamic in the Java language In fact when invokedynamic was added to the runtime in Java 7 the javac compiler would not emit the new bytecode under any circumstances whatsoever As of Java 8 invokedynamic is used as a primary implementation mechanism to provide advanced platform features One of the clearest and simplest examples of this use of the opcode is in the implementation of lambda expressions To follow along with the rest of this article youll need to have some familiarity with how the JVM invokes methods or youll need to read the first article in this series Lambdas Are Object References Before diving into how invokedynamic is used to enable lambdas a brief reminder of what lambdas actually are is in order Java has only two types of values primitive types such as char int and so on and object references Lambdas are obviously not primitive types so they must be object references Consider this lambda public class LambdaExample PHOTOGRAPH BY JOHN BLYTHE
You must have JavaScript enabled to view digital editions.