Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.albasani.net!.POSTED!not-for-mail From: Sebastian Newsgroups: comp.lang.java.programmer Subject: Java 8 closures vs. anonymous inner classes Date: Wed, 04 Dec 2013 20:07:42 +0100 Organization: albasani.net Lines: 24 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net vHEJoxTnuj7HJqvNdWYQhQ+GkWF6iC+Tk4N9HLHJ0Wh3wvJ7SlSqSLFAv8t55u2O7EO+zdwresQFvjGnrvI0Jw== NNTP-Posting-Date: Wed, 4 Dec 2013 19:08:15 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="lBoB/xulmu190ptBRRarjvKgooIERLw4SWKuk6/2m+bgSfo/NjWRzCEf+tcZwljYQlVS8U8dOqOECfh4rDX+pb7KmAHvK3Qja7WSAmuVcbrhlHIh/N49lLlvXgQtlkvf"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0.1 Cancel-Lock: sha1:kv2OipS1AIWYp6AcRFLLuaIBtB4= Xref: csiph.com comp.lang.java.programmer:26728 Someone made a passing comment in another thread to the effect that Java 8 lambdas ("closures") were nothing but syntactic sugar for (anonymous) inner classes. Here are a three differences: 1) lambdas can only instantiate "functional" (single abstract method) interfaces 2) lambdas have different scoping rules ("this" means different things) 3) lambdas perform better due to the use of the invokedynamic JVM instruction. Type representation, instance creation, and invocation strategy are decided at runtime. This offers optimization potentials, because evaluating a lambda expression need not create a new instance each time. There could be „shared lambdas“ through use of a constant static method in a constant pool, needing no new class and/or new object. This is quite a bit more than what I'd call "syntactic sugar". -- Sebastian