Java 17

 Lambda Expression

  • Interface with a single abstract method is called as functional interface
    • It is denoted with annotation @FunctionalInterface
  • Anonymous Classes, are created When we implement an interface in line, the implementation is hidden, and all we have is the reference to that implementation. that is the interface implementation name
  • Lambda is replacement of anonymous functions.
  • Lambda can only be used with functional interfaces.
  • Anonymous and inheritance implementation can be used with abstract classes.
  • Interface is a pure abstract class.
  • JSR or Java specification requests Must be followed while building a JDK.
  • JDBC Specs Was released for database drivers. It is filled with interfaces and has only two classes.
    • JDBC driver releases must be based on JDBC specifications.
  • Interfaces play a vital role in consistency while development.
  • Interface once written are not modified generally.
  • Lambda is an in line representation of a code.
  • If we are inheriting from a functional interface, our child interface, which is also a functional interface must not have any abstract methods in it.
    • We can have default methods.
  • Starting Java 9 We can write, a void main Inside interface to test other default functions and static methods.
  • Whenever we declare anything in interface, it becomes public static void by default.
  • Lambda expression is writing code with more symbols and less words
    • Makes our code very concise and easy to maintain.
  • When it comes to inheritance The child interface should not be functional. That is, it should not have abstract methods.
  • Whenever we declare anything in interface, it becomes public static void by default.
  • Var is a data type whose Type is revealed at run time.
    • We can’t use other data types with var In method parameters.
    • (int a,var b)->{} won’t compile
    • Var can only be used in lambda, they can’t be used in proper methods.
    • Var Was introduced in Java 9
  • Primitives are not the part of object oriented concept of Java.
    • Primitives Don’t have get class method() So assigning them to var Is risky As we can’t determine its time.
    • Better to use var Only for objects
    • Primitives are stored on top of stack(int,float,double,bool,byte)
    • Each primitive gets a stack of a particular size.
  • When we declare primitives within the brackets they define the scope/lifetime Of Premitive
  • Garbage collection does not works on primitives.
  • Garbage collection only works with reference types, which may be available outside the scope.
  • Primitives may Have scope in class or method.
  • It is always better to use wrapper over primitives As it supports generics.
  • We can declare variables in lambda, but they will not be exposed outside.
  • Controlling of threads Is done by JVM Thread scheduler.
  • JVM consist of four parts
    • Class loader
    • Garbage collector
    • Thread Scheduler
    • Programme Access memory
      • Program managed area.
  • Thread scheduler Is responsible for managing lifetime of threads.
  • NDA-Non Deterministic Automata.
  • Default methods and multiple inheritance
    • Default methods are also called as defender methods or virtual extension methods.
  • We can override and overload default methods.
  • When we implement two interfaces with same default method, then we have to override one of the methods.
  • String builder in thread safe String buffer is not.
  • Static methods can’t be overridden. They must be called with interface name as they are not inherited.
  • Predicate Joining
  • EJB(Enterprise Archive(EJB))
Predicates and Method Reference
  • A lambda expression That evaluate to either true or false is called as predicate.
  • Predicate interface is inside, Java util functions package.
  • Predicate can be passed as a parameter to a method.
  • Whenever we declare in interface, everything becomes public, static final.
  • We can join predicates using “and”,”or”,”negate”.
  • Predicates are very useful for doing testing.
  • These are used for testing boundary value conditions.
  • Types of Packaging distributable in java.
    • Java Archive(non exec, runnable)
      • runnable has void main.
    • War
      • Web Archive
    • Ear
      • Enterprise Archive(EJB)
  • Function
    • A predicate which returns more than boolean That is any Java type is called as function.
      • A function can return any java object.
    • It is a predefined interface to return a response.
    • Bifunction Can take two Inputs and returns an output.
    • To send multiple values to a function, we use list or an array.
    • Functions are anonymous. They take parameters and return variables.
    • Predicate Takes An input type parameter and return boolean.
    • Function can take two type parameters input type And output type.
      • Second type parameter is return type.
    • Function calls apply to execute and Predicate calls test to execute.
    • Everything predicate can do function can do. But what function can do predicate cannot do.
      • Predicates can only return Boolean values. Functions can return any value.
    • Unit tests Will have no effect as far as lambda is concerned.
  • Method Reference
    • We use “::” For method reference in Java.
    • We have a functional interface, and there is a class method whose signature exactly matches the functional interface.
    • If there already exist a method with same signature, then we do not have to rewrite the method.
    • For example, we use “tostring()” method As reference from class. We also use New to create object as reference.
    • Reference means we are pointing, we are giving pointer to that method.
    • Reference can be made to static or non-static methods.
    • Using interface “disposed” within our class we can write destructor in Java class. Dispose is the destructor in Java class.
    • If we prioritise a thread in Java There is no guarantee that it will be prioritised. It all depends on JVM.
      • Set priority before we start our thread.
    • There is no guarantee that after writing destructor, garbage collector will run or not.
      • Finalise a method will not guarantee of GC run.
    • Converting, runnable to lambda.
      • It can be done by anonymous functions,Runnable Interface, Lambda, Method reference.
    • We should not use lot of static in web applications.
      • They are very dangerous
        • Web applications like spring, boot, batch, et cetera
        • Even for JSP and servlets
    • Applets are not supported by Modern JDK’s
    • When we start a thread From a thread It is called as spawning.
    • Child, thread Cannot be synchronised.
    • If we want a thread As demon thread Using setDemon() Property, it will run as background thread And response will not be displayed.
    • When using interface for method, reference to create object it just act as a mediator between program and class.
      • Program sends value to interface and interface calls the constructor.
    • It is used in factory patterns.
      • Returns the child class or implemented class Based on the parameters provided For example, we return interface with name database in abstract implementation.
      • Based on the parameters passed, we return the concerned implemented class Postgres,MySQL etc
      • Method references are best used in lambda’s.
      • When a class, which is inheriting in which the constructor is overloaded Both in parent and child. In the child constructor, we have to account for parent constructor parameters.

No comments:

Post a Comment