{"id":12915,"date":"2015-06-04T10:00:52","date_gmt":"2015-06-04T10:00:52","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=12915"},"modified":"2020-09-01T07:07:33","modified_gmt":"2020-09-01T07:07:33","slug":"java-8-lambda-expressions","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/java-8-lambda-expressions\/","title":{"rendered":"Java 8 Lambda Expressions"},"content":{"rendered":"<p>The Java programming language introduced in 1996 by Sun Microsystems as a cross platform environment and an object oriented programming structure has undergone tremendous changes.\u00a0 From introducing inner classes, JDBC, RMI, Annotations, the latest addition to the Java world is the entry of Lambda expressions. This post will provide an overview of \u2018Lambda expressions\u2019 and why it is necessary.<\/p>\n<p><strong>Missing feature of Java:<\/strong><\/p>\n<p>Java the strictly object oriented programming language makes one think only in terms of objects and classes. Given that time has evolved and more languages have come into existence with a different set of features \u2013 the creators and modifiers of Java felt the need to fill certain gaps and keep it updated with the current times. The \u2018functional programming\u2019 aspect of other languages was the compelling feature that was loudly missing in the Java programming world. As an example Javascript is a functional and object oriented programming language.<\/p>\n<p><strong>Features of Functional programming language:<\/strong><\/p>\n<p>The most important feature of a functional programming language is the \u201c<strong><em>closure\u201d<\/em><\/strong> property. The \u201cclosure\u201d property enables a function to access the variables in its function as well as the variables in the \u201cparent scope\u201d as well as all the global variables. Let us see an illustration of this in Javascript:<\/p>\n<pre>&lt;html&gt;\n &lt;head&gt; &lt;\/head&gt;\n &lt;body&gt;\n &lt;script&gt;\n function numbers(num1, num2){\n alert(\"Javascript closure example\");\n \/\/Inner function\n function show(){\n num1=num1+1;\n alert(num1+num2);\n }\n show();\n }\n numbers(1,2);\n &lt;\/script&gt;\n &lt;\/body&gt;\n &lt;\/html&gt;<\/pre>\n<p>In the above code, the inner function \u201cshow\u201d has access to the variables within it as well as the variables in the \u201cparent function\u201d as well.<\/p>\n<p><strong>Lambda expressions:<\/strong><\/p>\n<p>With the introduction to functional programming and closure property, we will now move to the real crux of this post \u2013 Lambda expressions.<\/p>\n<p>A Lambda expression is almost like an anonymous class \u2013 but it isn\u2019t. Lambda expressions also do not have a name, access modifier or return type. It is also implemented in the same place it is declared. It is also good for methods that are going to be used only once.<\/p>\n<p>Syntax of Lambda expressions:<\/p>\n<p>(arguments) -&gt; {body}<\/p>\n<ol>\n<li>There can be zero, one or more parameters<\/li>\n<li>If there are zero parameters, they have to be represented by empty braces(like \u2018()\u2019)<\/li>\n<li>If the body has a single statement \u2013 it need not be enclosed in braces<\/li>\n<li>However, if the body has more than statement, then it has to be enclosed in curly braces.<\/li>\n<\/ol>\n<p>Let us have a look at the following example of Lambda expressions:<\/p>\n<pre>\u00a0\/\/Program to illustrate Lambda expressions\npackage javaapplication1;\npublic class JavaApplication1 {\n\u00a0\u00a0\u00a0 public static void simple(String s1){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int i=0;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while(i&lt;5){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Lambda expression\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Runnable r=() -&gt; System.out.println(\"Hello\u00a0 \" +s1 );\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new Thread(r).start();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i++;\n\u00a0\u00a0\u00a0 }\u00a0\u00a0\n\u00a0\u00a0\u00a0 }\n\u00a0\u00a0\u00a0 public static void main(String [] args) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 simple(\"Whizlabs\");\n\u00a0\u00a0\u00a0 }\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n}<\/pre>\n<p>We are using the <em>functional interface<\/em> \u2018Runnable\u2019 to illustrate the working of our Lambda expression. The Lambda expression is used to start a thread and print the message \u201cHello Whizlabs\u201d five times. Note that, the variable is in the parent scope and it is easily available in our Lambda expression.<\/p>\n<p>This will be the output of our program:<\/p>\n<p>run:<br \/>\nHello\u00a0 Whizlabs<br \/>\nHello\u00a0 Whizlabs<br \/>\nHello\u00a0 Whizlabs<br \/>\nHello\u00a0 Whizlabs<br \/>\nHello\u00a0 Whizlabs<br \/>\nBUILD SUCCESSFUL (total time: 1 second)<\/p>\n<p>Note that a functional interface is an interface which has only one abstract method defined in it.<\/p>\n<p>We saw Lambda expressions and functional programming in this post. We will explore more Java related topics in future posts.<\/p>\n<p><span>Preparing for OCPJP 8 Certification? Pass in 1st attempt through <\/span><a href=\"https:\/\/www.whizlabs.com\/ocpjp-scjp\/\">Whizlabs OCPJP 8 Practice Test<\/a><a href=\"https:\/\/www.whizlabs.com\/six-sigma-green-belt-certification-course\/\"> ! <\/a>\u00a0<span>Start with Free Trial!<\/span><\/p>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Java programming language introduced in 1996 by Sun Microsystems as a cross platform environment and an object oriented programming structure has undergone tremendous changes.\u00a0 From introducing inner classes, JDBC, RMI, Annotations, the latest addition to the Java world is the entry of Lambda expressions. This post will provide an overview of \u2018Lambda expressions\u2019 and why it is necessary. Missing feature of Java: Java the strictly object oriented programming language makes one think only in terms of objects and classes. Given that time has evolved and more languages have come into existence with a different set of features \u2013 the [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[968,1014,1150],"class_list":["post-12915","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-8","tag-lambada-expression","tag-ocpjp-8"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":1,"uagb_excerpt":"The Java programming language introduced in 1996 by Sun Microsystems as a cross platform environment and an object oriented programming structure has undergone tremendous changes.\u00a0 From introducing inner classes, JDBC, RMI, Annotations, the latest addition to the Java world is the entry of Lambda expressions. This post will provide an overview of \u2018Lambda expressions\u2019 and&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=12915"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12915\/revisions"}],"predecessor-version":[{"id":75990,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12915\/revisions\/75990"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=12915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=12915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=12915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}