Contact@itspring.com
  • Difference Between Java 7 and Java 8

    There are some  key differences between Java 7 and Java 8.Now we will consolidate some of the features added in Java 8. 

    1. Lambda Expression : 
     
    Lambda expressions are new feature of Java 8 with in Lambda Expressions. Java lambda expression is a function  can be created without belonging to any class. Lambda expression is an anonymous function which allows us to pass methods as arguments without having any access modifier(private, public or protected), no return type declaration and no name.  It doesn't have any access modifier, no return type declaration and not even name.
     
    Lets take a look at this example. It will return the addition of a and b.
     
    (int a, int b) -> {return a + b} .
     
     
    Take a look at following e.g.
     
    Here we have a Calculator class in which we are performing basic operations like addition, substraction etc. and also printing a message using Lambda Expression.

    'MsgService' is in an interface and on the fly at main method, we are providing an implementation for that interface. After implementation, we use that instance and invoke its single methods. There will be always only one method and so the lambda expression need not specify the method name. 

    After executing following code you will get output as shown in the image.

    <p>&nbsp;//Java program Lambda Expression</p>
    <div>&nbsp;</div>
    <
    div>public class Calculator {</div>
    <
    div>&nbsp;</div>
    <
    div>&nbsp; &nbsp;public static void main(String args[]) {</div>
    <
    div>&nbsp; &nbsp; &nbspCalculator calc = new Calculator();</div>
    <
    div><span style="white-space:pre">        </span></div>
    <
    div>&nbsp; &nbsp; &nbsp//with type declaration</div>
    <div>&nbsp; &nbsp; &nbspMathCalculation addition = (int aint b) -&gtb;</div>
    <
    div><span style="white-space:pre">        </span></div>
    <
    div>&nbsp; &nbsp; &nbsp//with out type declaration</div>
    <div>&nbsp; &nbsp; &nbspMathCalculation subtraction = (ab) -&gtb;</div>
    <
    div><span style="white-space:pre">        </span></div>
    <
    div>&nbsp; &nbsp; &nbsp//with return statement along with curly braces</div>
    <div>&nbsp; &nbsp; &nbspMathCalculation multiplication = (int aint b) -&gt; { return b; };</div>
    <
    div><span style="white-space:pre">        </span></div>
    <
    div>&nbsp; &nbsp; &nbsp//without return statement and without curly braces</div>
    <div>&nbsp; &nbsp; &nbspMathCalculation division = (int aint b) -&gtb;</div>
    <
    div><span style="white-space:pre">        </span></div>
    <
    div>&nbsp; &nbsp; &nbspSystem.out.println(&quot;= &quot; + calc.operate(55addition));</div>
    <
    div>&nbsp; &nbsp; &nbspSystem.out.println(&quot;= &quot; + calc.operate(55subtraction));</div>
    <
    div>&nbsp; &nbsp; &nbspSystem.out.println(&quot;5 x 5 = &quot; + calc.operate(55multiplication));</div>
    <
    div>&nbsp; &nbsp; &nbspSystem.out.println(&quot;= &quot; + calc.operate(55division));</div>
    <
    div><span style="white-space:pre">        </span></div>
    <
    div>&nbsp; &nbsp; &nbsp//without parenthesis</div>
    <div>&nbsp; &nbsp; &nbspMsgService msgService message -&gt;</div>
    <
    div>&nbsp; &nbsp; &nbspSystem.out.println(&quot;\nGood Morning &quot; + message);</div>
    <
    div>&nbsp; &nbsp; &nbspmsgService.passMessage(&quot;Chandler&quot;);&nbsp;&nbsp;</div>
    <
    div>&nbsp; &nbsp;}</div>
    <
    div>&nbsp; &nbsp;//interface for MathCalculation</div>
    <div>&nbsp; &nbsp;interface MathCalculation {</div>
    <
    div>&nbsp; &nbsp; &nbspint operation(int aint b);</div>
    <
    div>&nbsp; &nbsp;}</div>
    <
    div><span style="white-space:pre">    </span>//interface for msg printing</div>
    <div>&nbsp; &nbsp;interface MsgService {</div>
    <
    div>&nbsp; &nbsp; &nbspvoid passMessage(String message);</div>
    <
    div>&nbsp; &nbsp;}</div>
    <
    div><span style="white-space:pre">    </span></div>
    <
    div>&nbsp; &nbsp;private int operate(int aint bMathCalculation mathCalculation) {</div>
    <
    div>&nbsp; &nbsp; &nbsp; return mathCalculation.operation(ab);</div>
    <
    div>&nbsp; &nbsp;}</div>
    <
    div>}</div>

Most Liked Posts

Post A Comment

Your email address will not be published. Required fields are marked*

JP5F

0 Comments

No Record found
demo content

The Total Economic Impactâ„¢ of ServiceNow

United States Tue Jun 06 2017

Join us for a webinar where we will reveal the quantified value of the Lightspeed Enterprise for customers that are modernizing IT, eliminating service outages, and optimizing performance with real-time analytics.

Guest speakers Liz Witherspoon of Forrester, and study participant Brad Paubel, VP of Internal Technologies at Maritz, will discuss

  • The value drivers for IT and key quantified economic benefits
  • Key challenges that study customers faced and overcame
  • Focus areas and best practices you can use to drive business impact with ServiceNow

Register

Webinar Registration

Enter the characters visible in the box below

52JL

Share

Share using

Advertise With Us