Java 1.8



    1. Why java 8
    2. New Features of Java 8
    3. Functional Programming vs OOP
    4. Is java 8 is a Functional Programming language
    5. Official Documentation of Java 8



1
2. Nested classes
    1. Anonymous classes




7
    1. What is a Functional Programming
    2. Examples of FP & OOP
    3. Why Lambda Expression
    4. Different syntaxes of Lambda Expressions.



3
  1. Functional Interfaces
1
  1. Method references (::) and Constructor reference (::)
3
  1. Interface vs Abstract classes
1
  1. Streams
4
  1. Default methods inside interface
1
  1. Static methods inside interface
1
  1. Java inbuilt functional interfaces (Predicate, Function, Consumer, Supplier)

5
  1. Data & Time API (JODA api)
1


1.  Why java 8
1. Why java 8?
  1. Performance improvements in common data structures.
  2. Fork/Join speed improvements.
  3. Changes to support concurrency.
  4. Lambda expressions, the Streams API, and new methods on existing classes are some of the key productivity improvements.
  5. New Optional type gives developers significant flexibility when dealing with null values, reducing the likelihood of NullPointerExceptions.
  6. Garbage Collector Improvements.
  7. Easy to Parallelize.

2. What are the new features of java 8?
  1. Lambda expressions.
  2. Functional Interfaces.
  3. Method references.
  4. Methods inside interface.
  5. Java inbuilt functional interfaces.
  6. Streams.
  7. Date & Time (Joda API).

3. Functional Programming (FP) vs Object Oriented Programming (OOP)?
OOP :
  1. OOP is most successful programming approach.
  2. Object contains ‘State’ and ‘Behaviour’, State is variables and Behaviour is methods.
  3. OOP it tightly couples the state and behaviour of object together.
  4. Data (state) of the object will be updated by the methods(behaviour) of the object.
FP :
  1. Idea behind the FP is mathematics, Functional Programming is a form of programming which considers computation as an evaluation of mathematical functions.
  2. The foundation for functional Programming is Lambda Calculus. Lambda calculus was developed in 1930s to be used for functional definition, functional application and recursion in mathematics.
  3. LISP was the first functional programming language. It was defined by McCarthy in 1960.
  4. Lisp is predominantly used in the area of Artificial intelligence and as a macro language on top of softwares like AutoLISP for AutoCad and elisp for emacs.
  5. It avoids the changing of data, the data is immutable.
  6. Some inputs are transformed to outputs without modifying input data.
  7. FP supports parallel processing by multi core support.
4. Is java 8 is a Functional Programming language ?
  1. JVM languages like scala, clojure and frameworks like guava supports functional programming, and mostly used in data processing like hadoop.
  2. By inspiring those languages Java 8 is trying transforming into functional programming support along with OOP.

5. Official documentation of Java 8



2.  Lambda Expressions
1. What is a Functional Programing?
  1. Characteristics of FP
    1. Function Closure Support.
    2. Higher-order functions
    3. Use of recursion as a mechanism for flow control
  2. Examples of FP languages : scala, erlang, LISP, clojure.
  3. Examples of FP code
Function powerFunctionFactory(int power) {
  int powerFunction(int base) {
      return pow(base, power);
  }
  return powerFunction;
}
Function square = powerFunctionFactory (2);
square(3); // returns 9
Function cube = powerFunctionFactory (3);
cube(3); // returns 27
  1. FP supports closures (closure is a method reference or anonymous function).
  2. FP supports currying ‘->’( Given a function foo(x,y) which results in the value of z, better expressed foo(x,y) -> z)

2. What is a Closure?
  1. When you declare a local variable, that variable has a scope. Generally local variables exist only within the block or function in which you declare them.
function() {
 var a = 1;
 console.log(a); // works
}    
console.log(a); // fails
  1. If I try to access a local variable, most languages will look for it in the current scope, then up through the parent scopes until they reach the root scope.
var a = 1;
function() {
 console.log(a); // works
}    
console.log(a); // works
  1. a closure is the ability for a function to access variables defined in the same execution scope as the function is defined.
var a = "hello";
var f1 = function(){
   print (a);
}
a = "man";
f1();    // will result to "man";
  1. A closure is a persistent scope which holds on to local variables even after the code execution has moved out of that block.

3. What is a Higher-order functions?
  1. Higher-order function is a function that does at least one of the following:
    1. takes one or more functions as arguments (i.e., procedural parameters)
    2. returns a function as its result.
  2. Example of Higher-order function
Takes one or more functions as arguments
var proveIt = function() {
 alert("you triggered " + this.id);
};

document.getElementById("clicker").addEventListener("click", proveIt);

Returns a function as its result
outer = function() {
 var a = 1;
 var inner = function() {
   console.log(a);
 }
 return inner; // this returns a function
}

var fnc = outer(); // execute outer to get inner
fnc();


4. Examples of Functional Programing and Object Oriented Programing?

Java 7 (OOP)
Java 8 (FP & OOP)
Thread thread = new Thread(new Runnable() {
   @Override
   public void run(){
       System.out.println("Task #1 is running");
   }
});
thread.start();
// Lambda Runnable
Runnable r = () -> { System.out.println("Task #2 is running"); };
// start the thread
new Thread(r).start();

5. Why Lambda Expression?
  1. Java lambda expressions are Java's first step into functional programming.
  2. A Java lambda expression is thus a function which can be created without belonging to any class.
  3. A lambda expression can be passed around as if it was an object and executed on demand.
  4. Simple to write, easy to understand.
  5. Passing a lambda expression to another function allow us to pass not only values but also behaviors and this enable to dramatically raise the level of our abstraction and then project more generic, flexible and reusable API.




6. What is Anonymous class?
  1. Anonymous classes enable you to make your code more concise.
  2. They enable you to declare and instantiate a class at the same time.
  3. They are like local classes except that they do not have a name.Use them if you need to use a local class only once.
  4. While local classes are class declarations, anonymous classes are expressions, which means that you define the class in another expression.
interface HelloWorld {
       public void greet();
       public void greetSomeone(String someone);
}



HelloWorld frenchGreeting = new HelloWorld() {

           String name = "tout le monde";
           public void greet() {
               greetSomeone("tout le monde");
           }
           public void greetSomeone(String someone) {
               name = someone;
               System.out.println("Salut " + name);
         }
};

  1. Syntax of Anonymous Classes :
The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.
HelloWorld frenchGreeting = new HelloWorld() { ……………… };

  1. The anonymous class expression consists of the following:
    1. The new operator
    2. The name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface HelloWorld.
    3. Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.
    4. A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.
7. What is Lambda expression?
  1. One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear.
  2. If interface contains only one method, then also we need to mention the method name in anonymous class declaration.
interface Runnable {
       public void run();
}



Runnable runnable = new Runnable() {
           public void run() {
               System.out.println(“i am a new thread”);
           }
 };

  1. In above example, i am creating an implementation class (anonymous class) of Runnable interface, but still i need to mention method name ‘run()’.
  2. In this case by using lambda expression, we can directly pass functionality as an argument to another method to treat functionality as method argument, or code as data.
interface Runnable {
       public void run();
}



Runnable runnable = () ->  System.out.println(“i am a new thread”);

  1. We can use lambda expression, if the interface contains only one method.
  2. A lambda expression can be passed around as if it was an object and executed on demand.

8. Syntaxes of Lambda expressions
  1. Lambda expressions supports interfaces only, Creating an instance to implementation class of a functional interface (interface contains only one method).
  2. Suppose java.lang.Runnable interface is a functional interface, it contains only one method called ‘run()’.
  3. We can assign a block of code or method without a name to variable of type interface directly by using lambda expressions.
          Runnable runnable = () -> {
               System.out.println(“i am a new thread”);
           };
  1. Block of code contains syntax like this  :       
    (arguments list)  ->  { block of code };
  2. In lambda expression, no need to mention the name of the block.
  3. We can pass the list of arguments with or without data type mentioning.
  1. But recommended way is don’t mention data type, java compiler will detect the data type of the argument by looking into the functional interface.
  2. Suppose if we mention wrong data type (other than data type declared in interface method) it will give compile time error.
  3. It will also not consider inheritance relationship also, we need to mention exact type, not child type or not parent type, otherwise don’t mention any type it will add type.
  1. Java compiler automatically detects argument types and return type of the lambda expression.
  2. No need to mention the return statement also, by default it returns last statement.
  3. If you write last statement as improper, it gives compile time error.
  1. If you want we can also mention return statement, but it is optional to mention, but if you mention return statement need to write ‘{ }‘ code only.

  1. If block contains only one line of code than optional to use ‘{‘.

9. Rules for Lambda expressions
  1. We can create anonymous class for an interface or abstract class or class with any number of methods, but lambda expressions can only supports functional interfaces(interface contains only one method).
ANONYMOUS CLASS






LAMBDA EXPRESSION
*Lambda expressions for abstract classes and classes it is giving error.
  1. We need to mention a correct data type or don’t mention the data type, just declare a variable, compiler automatically add data type to that variables.




=
  1. Some examples of lambda expression
MyInterface myInterface = (a) -> {return a.indexOf('i');}
MyInterface myInterface = (a, b ) -> a+b;
MyInterface myInterface = (a) -> a*b;
MyInterface myInterface = (int a, int b ) -> a+b;
MyInterface myInterface = (int a, int b ) -> {
        return a+b;
};
  1. If you mentioned any data type for one argument, you need to mention remaining args also.



6. What is Nested classes?
  1. The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:
class OuterClass {
   ...
   class NestedClass {
       ...
   }
}
  1. Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.
class OuterClass {
   ...
   static class StaticNestedClass {
       ...
   }
   class InnerClass {
       ...
   }
}
  1. A nested class is a member of its enclosing class.
  2. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.
  3. Static nested classes do not have access to other members of the enclosing class.
  4. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)

7. Why Nested classes?
  1. It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
  2. It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
  3. It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
//Outter class, enclosing class
public class OutterClass {

//Inner class, nested class
//Nested class, static nested class
static class StaticNestedClass {

}

//Inner class, nested class
//Nested class, non static nested class, inner class
class InnerClass {

}
}

8. Types of Nested classes?
  1. There are four kinds of nested class in Java.
    1. static class: declared as a static member of another class
    2. inner class: declared as an instance member of another class
    3. local inner class: declared inside an instance method of another class
    4. anonymous inner class: like a local inner class, but written as an expression which returns a one-off object

9. How to create an object for static nested classes?
  1. By using outer class name, we can create object for static nested class.
class OuterClass {
   ...
  static  class InnerClass {
       ...
   }
}

OuterClass.StaticInnerClass staticInnerClass = new OuterClass.StaticInnerClass();
  1. We can’t create object for static nested class with outer class reference.
OuterClass outerClass = new OuterClass();
OuterClass.StaticInnerClass staticInnerClass2 = new outerClass.StaticInnerClass();
  1. With in the Outer class we can directly create object without outer class name
OuterClass outerClass = new StaticInnerClass();
  1. In static nested class we can define both static members and instance members.




package com.nested;

public class OuterClass {
int i = 1;
static int j = 1;
public void test() {
//directly calling static members from outer class
main();
new StaticInnerClass();
new OuterClass.StaticInnerClass();
//Print static member of static nested class
System.out.println(StaticInnerClass.j);
//Print instance member of static nested class
System.out.println(new StaticInnerClass().i);
}

public static void main(){
System.out.println("static main");
}
public static class StaticInnerClass {
int i = 2;
static int j = 2;
}
}


package com.nested;

public class Main {
public static void main(String[] args) {
//Create an object for outer class
OuterClass outerClass = new OuterClass();
//Create an object for StaticInnerClass class
OuterClass.StaticInnerClass staticInnerClass = new OuterClass.StaticInnerClass();
System.out.println(staticInnerClass.i);
System.out.println(OuterClass.StaticInnerClass.j);
System.out.println(OuterClass.j);
System.out.println(outerClass.i);
}
}

10. How to create an object for non static nested classes or Inner class?
  1. By using outer class refernce, we can create object for non static nested class or inner class.
class OuterClass {
   ...
   class InnerClass {
       ...
   }
}

OuterClass outerClass = new OuterClass();
OuterClass.StaticInnerClass staticInnerClass2 = outerClass.new StaticInnerClass();
  1. With in the Outer class we can directly create object without outer class name
OuterClass outerClass = new StaticInnerClass();
  1. In non static nested class we can define only instance members.

package com.nested;

public class OuterClass {
int i = 10;
static int j = 20;

void test() {
System.out.println("instance test");
System.out.println(new InnerClass().i);
new InnerClass().test();
System.out.println(new OuterClass.InnerClass().i);
}

class InnerClass {
int i = 100;
void test(){
System.out.println("I am in test InnerClass "+i);
System.out.println("I am in test OuterClass "+OuterClass.this.i);
}
}
}


package com.nested;

public class Main {
public static void main(String[] args) {
OuterClass o1 = new OuterClass();
o1.test();
//OuterClass.test();
OuterClass.InnerClass i1 = o1.new InnerClass();
}
}

11. Static nested class vs non static nested class?
Static Nested class
Non Static nested class (Inner class)
Static nested class object can be created through Outer class name only
Non static nested class can be created through Outer class reference only
Inside Static nested class we can declare both static and non static members
Inside non static nested class we can declare only non static members
OuterClass.StaticInnerClass i1 = new OuterClass.StaticInnerClass();
OuterClass o1 = new OuterClass();
OuterClass.InnerClass i1 = o1.new InnerClass();
We can’t get Outer class reference
We can get outer class reference (OuterClass.this)

12. How to create an object for Local Inner class?
  1. Local inner class can define in any block or method either static or non static (within loops or expressions also possible)
  2. Local inner class can have only instance members, no static members for except in below condition
  3. Local inner class can contains final static variables, not methods
  4. Local inner class can have only instance methods, blocks
  5. Local inner class can access outer class static, non static members if and only if block or method is non static
  6. Local inner class can access outer class static members only if and only if block or method is static
  7. Local inner class can be accessed in within the outer class method or block scope only
public class OuterClass {
int i = 10;
static int j = 20;

void test() {
class InnerClass {
int i = 100;
static final int j = 200;
void innerTest() {
System.out.println("I am in test InnerClass " + i);
System.out.println("I am in test OuterClass " + OuterClass.this.i);
System.out.println("I am in test OuterClass " + OuterClass.j);
}
}
System.out.println(InnerClass.j);
System.out.println(new InnerClass().i);
new InnerClass().innerTest();
}
public static void main(String[] args) {
new OuterClass().test();
}
}



13. Nested interface
  1. An interface i.e. declared within another interface or class is known as nested interface.
  2. The nested interfaces are used to group related interfaces so that they can be easy to maintain.
  3. The nested interface must be referred by the outer interface or class. It can't be accessed directly.
  4. Nested interfaces are declared static implicitly.
interface interface_name{  
...  
interface nested_interface_name{  
 ...  
}  
}   
class class_name{  
...  
interface nested_interface_name{  
 ...  
}  
}   
  1. We can’t declare interface in a block or a method.
  2. We can also declare a class inside a interface, it is static nested class
package com.nested;

public class Main {
public static void main(String[] args) {
Main.Test m1 = new Main.StaticTestImpl();
Main.Test m2 = new Main().new TestImpl();
m1.print();
m2.print();
}
interface Test{
int a =1;
void print();
class MyTest{
}
}
static class StaticTestImpl implements Main.Test{
@Override
public void print() {
System.out.println(this+"  "+a);
abstract class AbstractTest{
abstract void print();
}
}
}
class TestImpl implements Main.Test{
@Override
public void print() {
System.out.println(this);
}
}
}


No comments:

Post a Comment

3. Java Program to create Binary Tree