Java Magazine, Nov/Dec 2017
ORACLE COM JAVAMAGAZINE NOVEMBER DECEMBER 2017 68 inside the jvm private static final String HELLO Hello public static void main String args throws Exception Runnable r System out println HELLO Thread t new Thread r t start t join The lambda expression on line 5 is assigned to a variable of type Runnable This means that the lambda evaluates to a reference to an object that has a type that is compatible with Runnable Essentially this objects type will be some subclass of Object that has defined one extra method and has no fields The extra method is understood to be the run method expected by the Runnable interface Before Java 8 such an object was represented only by an instance of a concrete anonymous class that implemented Runnable In fact in the initial prototypes of Java 8 lambdas inner classes were used as the implementation technology The long range future roadmap for the JVM could contain future versions where moresophisticated representations of lambdas could be possible Fixing the representation to use explicit inner classes would prevent a diferent representation being used by a future version of the platform This is undesirable and so instead Java 8 and Java 9 use a more sophisticated technique than hardcoding inner classes The bytecode for the previous lambda example is as follows public static void main java lang String throws java lang Exception Code 0 invokedynamic 2 0 InvokeDynamic 0 run Ljava lang Runnable
You must have JavaScript enabled to view digital editions.