Saturday, 5 March 2016

Lambda Expressions



Lambda Expression:

Java 8 introduced many features in its new version, amongst them stands lambda expressions. The way of writing the methods have been changed. Especially for loop is more generalized.

Advantages of lambda:

1.      They add new syntax elements.
2.      Parallel processing can be done (more detail in the parallelism section).
3.      It supports pipeline operations on data.
4.      Appending default functionality to the method that is defined in interface, default functionality executes on the reference of the interface method, surprisingly the method is not executed.

Implementation:

    Implementation of java includes two constructors:

1.      Lambda expression
2.      Functional interface.

Key points to remember:

1.      Lambda expression is anonymous method.
2.      This method don’t execute o its own, rather used to execute a function or method that is defined in the functional interface.
3.      They are called as ‘closures’.
4.      Lambda expression is redefined in the form of anonymous classes.
5.      We can only use lambda expression if and only if target type is known or specified.

Functional interface (Single Abstract Method Type):

1.      By name it’s an interface that contains only one method, which is abstract.
2.      It defines the target type of the lambda expression.

Lambda expression:

1.      A new operator is introduced, operator is called as ‘lambda operator’ or ‘arrow operator’. It is represented by ‘->’. This sign is verbally called as ‘goes to’ or ‘becomes’.
2.      The left side of the LE is used to specify parameters and the right side is for lambda body.
3.      Lambda body is of two types:
1.      Consisting single expression.
2.      Consisting a block.



marker interfaces

                                                            Marker Interfaces in Java


"An interface is called a marker interface when it is provided as a handle by Java interpreter to mark a class so that it can provide special behaviour to it at 
runtime and they do not have any method declarations". 

In the above definition "and they do not have any method declarations" is added because, till now all the marker interfaces provided by Java do not have method 
declarations. 

We cannot create marker interfaces, as you cannot instruct JVM to add special behaviour to all classes implementing (directly) that special interface. 



We don't need to create your own marker interface since it is not recommended by java. For your knowledge I want to make you clear that their 
is no any definition for marker interface in Java specification/api. Whatever we study on web/books is cooked by web developers. 

Now since jdk 1.5 we dont need to use marker interface, in place of this we can use annotations which have their own many advantages.