{"id":3240,"date":"2019-03-17T12:37:05","date_gmt":"2019-03-17T12:37:05","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3240"},"modified":"2019-03-17T16:25:12","modified_gmt":"2019-03-17T16:25:12","slug":"lambda-expression-vs-anonymous-inner-class","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/lambda-expression-vs-anonymous-inner-class\/","title":{"rendered":"Lambda Expression vs Anonymous Inner Class"},"content":{"rendered":"<p>Lambdas implement a functional interface.Anonymous Inner Classes can extend a class or implement an interface with any number of methods.<br \/>\n<strong>Variables<\/strong> \u2013 Lambdas can only access final or effectively final.<br \/>\n<strong>State<\/strong> \u2013 Anonymous inner classes can use instance variables and thus can have state, lambdas cannot.<br \/>\n<strong>Scope<\/strong> \u2013 Lambdas can&#8217;t define a variable with the same name as a variable in enclosing scope.<br \/>\n<strong>Compilation<\/strong> \u2013 Anonymous compiles to a class, while lambda is an invokedynamic instruction.<\/p>\n<p><strong>Syntax<\/strong><br \/>\nLambda expressions looks neat as compared to Anonymous Inner Class (AIC)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String&#x5B;] args) {\r\n    Runnable r = new Runnable() {\r\n        @Override\r\n        public void run() {\r\n            System.out.println(&quot;in run&quot;);\r\n        }\r\n    };\r\n\r\n    Thread t = new Thread(r);\r\n    t.start(); \r\n}\r\n\r\n\/\/syntax of lambda expression \r\npublic static void main(String&#x5B;] args) {\r\n    Runnable r = ()-&gt;{System.out.println(&quot;in run&quot;);};\r\n    Thread t = new Thread(r);\r\n    t.start();\r\n}\r\n<\/pre>\n<p><strong>Scope<\/strong><br \/>\nAn anonymous inner class is a class, which means that it has scope for variable defined inside the inner class.<\/p>\n<p>Whereas,lambda expression is not a scope of its own, but is part of the enclosing scope.<\/p>\n<p>Similar rule applies for super and this keyword when using inside anonymous inner class and lambda expression. In case of anonymous inner class this keyword refers to local scope and super keyword refers to the anonymous class\u2019s super class. While in case of lambda expression this keyword refers to the object of the enclosing type and super will refer to the enclosing class\u2019s super class.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/AIC\r\n    public static void main(String&#x5B;] args) {\r\n        final int cnt = 0; \r\n        Runnable r = new Runnable() {\r\n            @Override\r\n            public void run() {\r\n                int cnt = 5;    \r\n                System.out.println(&quot;in run&quot; + cnt);\r\n            }\r\n        };\r\n\r\n        Thread t = new Thread(r);\r\n        t.start();\r\n    }\r\n\r\n\/\/Lambda\r\n    public static void main(String&#x5B;] args) {\r\n        final int cnt = 0; \r\n        Runnable r = ()-&gt;{\r\n            int cnt = 5; \/\/compilation error\r\n            System.out.println(&quot;in run&quot;+cnt);};\r\n        Thread t = new Thread(r);\r\n        t.start();\r\n    }\r\n<\/pre>\n<p><strong>Performance<\/strong><br \/>\nAt runtime anonymous inner classes require class loading, memory allocation, and object initialization and invocation of a non-static method while lambda expression is a compile-time activity and don\u2019t incur extra cost during runtime. So the performance of lambda expression is better as compare to anonymous inner classes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lambdas implement a functional interface.Anonymous Inner Classes can extend a class or implement an interface with any number of methods. Variables \u2013 Lambdas can only access final or effectively final. State \u2013 Anonymous inner classes can use instance variables and thus can have state, lambdas cannot. Scope \u2013 Lambdas can&#8217;t define a variable with the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/lambda-expression-vs-anonymous-inner-class\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[265],"tags":[],"class_list":["post-3240","post","type-post","status-publish","format-standard","hentry","category-concepts"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3240"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3240\/revisions"}],"predecessor-version":[{"id":3248,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3240\/revisions\/3248"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}