Tuesday, December 19, 2017

Java 8 Feature 1. Lambda Expressions


Lambda Expression:

You might have heard of lambda in mathematics.It is a bog change in math and is introduced in 1930’s.It solved complex problems easily.

Java is not first language to use lambda expression. First language which lambda expression is LISP.

There are other languages as well:
C, C++,objective c, c# .net, SCALA, ruby,python …. And Java.

Why Lambda ?

1. Lambda expression is to enable functional programming in Java.

2. To write more readable, maintainable and concise code.

3. To use APIs very easily and effectively.

4. To enable parallel processing.


There is wrong understanding in between developers that Lambda Expression has come to replace anonymous inner classes. Which is totally incorrect.








Basic difference between Anonymous Inner Class and Lambda Expression.



          Anonymous Class                                           lambda expression

  1. Class without name function without name

  1. Can extend abstract and can extend,can implement
Concrete class
    3.    Can implement interface with any          only 1 interface which contain single               abstract number of methods method (functional interface only)

4. We can declare instance variable can’t declare instance variable, whatever
Variable are considered as local variable.

5. Can be initiated can not

6. this always referer current inner    this always refers current outer class
Class object but not outer class object object

7. Best choice if want to handle multiple best choice to handle functional interface
  Methods.

8. For every class a separate .class file will no separate class
be generated internally

9. Memory will be allocated in heap inside method area. Inside permanent
Memory of JVM.




Demo Labmda Expression:

interface Interf {
public void m1();
}

public class Test1 {
int x=10;

public void m2(){

int y=20;

//lambda expression

Interf i = () -> {
System.out.println(x);
System.out.println(y);
};
i.m1();
}
public static void main(String[] args) {
Test1 t1 = new Test1();
t1.m2();
}
}



Any local variable which is referenced from lambda expression is always final whether we are declaring or not..In above sample code its y which is local and referenced from lambda expression.

This lambda expression is not executing inside the m2() method.But from t.m2()

You can not change local variable value which is y anywhere within or out of lambda expression.



If you are using local variable, then it becomes final.


Compile Error: Local variable y defined in an enclosing scope must be final or effectively final

If you are not using local variable in lambda expression, then its not final.




We can replace big code with small code and complexity using lambda expression.
Eg.

String s = “sks”;

Below highlighted is a function assigned to interface.  :)

Interf i = () -> System.out.println(“hello”);

We are in a position to pass lambda expression to the method as parameter and pass field in constructor.

Eg.

New Thread( () ->{
super(“child thread”);
}; )

Java 8 Feature and its purpose


Features:

Below mentioned are few features in java 8:

1. Lambda Expressions
2. Functional Interfaces   (type to call lambda expressions)
3. Default methods in interfaces (we can declare concrete methods here)  1.8 version onwards
4. Static methods in interfaces (also static inside interfaces) 1.8 version onwards
5. Predicate is  predefined functional interface
6. Function is predefined functional interface
7. Consumer is predefined functional interface
8. :: was in c++, but now in Java 8 with different purpose which is method reference and constructor reference.
9. Stream API :(Most Imp)  to perform bulk operation on collection becomes very easy. More readable and concise code in a single line instead of 6 lines.
10. Date and time API :  --  Introduced by joda.org-- Also known as Joda API


Purpose of java 8 features:

  1. To simplify programming.
  2. To enable/utilise functional programming benefits in Java in the form of Lambda expression.
  3. To enable parallel programming/processing in java.I can write more compatible code which is going to run on multi core systems. (overall performance is going to be improves).


Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...