Saturday 20 February 2016

Upgrading Eclipse from Juno to Kepler

Background

In this post I will show you how to upgrade your Eclipse from Juno to Kepler. My current installation details are as follows -



Upgrading Eclipse from Juno to Kepler

  • First you need to add update sites. So go to Windows -> preference. Then search for "Available software sites" and select Add... Then you need to add following 2 sites -
    1. Kepler release repo: http://download.eclipse.org/releases/kepler
    2. Kepler update repo: http://download.eclipse.org/eclipse/updates/4.3
  • Next go to Windows -> Check for Update

  • Previous step may take come time as it checks for new updates. Next select updates as follows -
  • Click Next, accept TnC and click finish to start installation of updates. After installation is complete simply restart Eclipse and you should get Eclipse Kepler.


Using Lambda Expressions from Java 8

Background

It's been some time since Java 8 is out and with it there are some major changes that have come in. Some examples are -
  • Lambda expression
  • Stream collection APIs
  • Functional interfaces etc.
In this post we will see what are Lambda expressions and how we use it in code. 

Introduction

Lambda expression is essentially an anonymous method. But it's not just any method. It is implementation of abstract method that is present in an functional interface. So lambda expression form a kind of anonymous class. Lambda expressions are also called closures.

We said lambda expression essentially implements an abstract method in an functional interface. But what is an functional interface. Functional interface is nothing but an interface with just one abstract method. For example consider interface Runnable . It has a single abstract method run(). So Runnable is a functional interface.

NOTE : A functional interface may specify any public method defined by Object, such as equals( ),
without affecting its “functional interface” status. The public Object methods are considered implicit
members of a functional interface because they are automatically implemented by an instance of a
functional interface.

Lambda expressions and function interfaces

Enough with the introduction. Lets see an example of lambda expression. Consider following interface - 

interface MyInterface {
       int getAge(); 
} 

Does above interface qualify as a functional interface? It certainly does. It just has one method and all methods in an interface and by default public and abstract [Note : There is an exception to this as since Java 8  you can specify default implementation in interface itself but for now I am going to keep default methods away from discussion. We can revisit it in some future post]. Ok So we have a functional interface. Now lets see how we can write a lambda expression for this.

MyInterface myInterface = () -> 24;
System.out.println(myInterface .getAge());

And you should get 24 printed in console. Before we go in the details of this expression lets first look how lambda expression is structure and read. Lambda expression used is - 

  () -> 24
'->' operator is called lambda operator or arrow operator. It divides your lambda expression into two parts. LHS of the operator specifies the arguments of the abstract method of the functional interface. RHS is the body of the method (the return value). RHS can either be a single line or a block of code. The lambda expression is read as "LHS goes to RHS".



NOTE : Single line lambda expressions are called expression lambdas where as the ones with block are called block lambdas.

Difference : Difference between expression and block lambdas is that block lambdas must have a return statement unlike expression lambdas where is it obvious (RHS).

Some more examples

Lets take some more examples to understand this better. Consider following functional interface

interface MyInterface {
       boolean isAllowedAge(int currentAge); 
} 

For this your lambda expression can be -

MyInterface myInterface = (n) -> n>=18;
System.out.println(myInterface.isAllowedAge(4));


Couple of points to note here -

  • Note we are no where specifying the type of n. It is automatically inferred from the functional interface. But if you wish you can specify it -

    MyInterface myInterface = (int n) -> n>=18;
    System.out.println(myInterface.isAllowedAge(4));
    


    However if you specify type of one arguments you should do that for all other arguments. So something like (int n, m) -> n>=m; is not allowed. Also the argument should qualify/match. So something like below wont work


    MyInterface myInterface = (int n) -> "test";   //will not work
    System.out.println(myInterface.isAllowedAge(4));
    
  • If you just have one argument then you don't need parenthesis on the LHS of lambda expression. So you can do something like - 
    MyInterface myInterface = n -> n>=18;
    System.out.println(myInterface.isAllowedAge(4));
    


    But it may get confusing at time. So it is better to always use a parenthesis even if you have one argument :)
Lets see a two argument lambda expression -

Your interface is

interface MyInterface {
       boolean isAllowedAge(int currentAge, int minAge); 
}


and your lambda expression would look something like -

MyInterface myInterface = (n,m) -> n>=m;
System.out.println(myInterface.isAllowedAge(4,5));

Block lambda expression for same interface would look like -

MyInterface myInterface = (n,m) -> {
    int minAge = m;
    if (n >= minAge )
        return true;
    else
        return false;
    }
System.out.println(myInterface.isAllowedAge(4,5));



As you must have noticed you can have local variables, loops, switch statement etc in your block body.

Another good example would be -

Comparator c = (a, b) -> Integer.compare(a.length(), b.length());

OR

Runnable myRunner= () ->{
    System.out.println("I am running");
}; 


NOTE : The default methods introduced in Java 8 do not affect the functional status of an interface. By definition functional interface just has one abstract method. It can have other default methods.

I am going to end up this post here that was intended for lambda expressions introduction. I know there are various follow up topis -

  1. Generic
  2. used as Method arguments
  3. Exception handling

I will come to these topics in subsequent posts.  So stay tuned :)

2nd Part of this tutorial updated -
NOTE  :While it is a good practice to mark a functional interface with the @FunctionalInterface
annotation for clarity, it is not required with functional programming. The Java compiler
implicitly assumes that any interface that contains exactly one abstract method is
a functional interface. Conversely, if a class marked with the @FunctionalInterface
annotation contains more than one abstract method, or no abstract methods at all, then
the compiler will detect this error and not compile.

NOTE : Remember that the parentheses are optional only when there is one parameter and it doesn’t have a type declared.  If you are not using braces you cannot say return something.


NOTE : any public method defined by Object, any default methods or any static methods do not affect the functional status of an functional interface. As long as it has just one abstract method.

 NOTE : If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. (JLS)

NOTE : Main thing to understand about Lambdas is deferred execution. This part of code will be executed at a later point of time.

NOTE : Lambdas use the same access rules as inner classes.
Lambda expressions can access static variables, instance variables, effectively final method parameters, and effectively final local variables. 

Related Links

t> UA-39527780-1 back to top