Thursday, 19 October 2017

java 8 introduction

1.     What's New in JDK 8
a.      Lambda Expressions
b.      Functional Interfaces
c.       Default methods                                                            
d.      Static Methods
e.      Method Reference and Constructor reference using double column operator(::)
f.        Stream API
g.      Date and Time API(Joda API)
2.     Purpose of Java 8
a.      To  simplify programming
                                                              i.      Less code
                                                            ii.      Maintainable
                                                          iii.      Readable
b.      To get the benefits of the functional program in java, Java uses lambda expressions.
c.       To enable parallel processing

3.      Lambda expressions
a.      Lambda expressions are methods which are do not belong to a class. They are just functions exist in isolation and that function can be treated as values.
b.      Functions as Values
                                                              i.      Normally, We can assign a string or any objects into a variable.
                                                            ii.      Lambda expression is  a method which has assigned to a variable. If we are assigning we do not need other method signature. So the lambda expression signature will be like below code,
aVariable = ()-> {System.out.println("HIIII");}
                                                          iii.      Lambada expressions with parameters.
lambdaWithParamater = (int x)->{x= x+}
c.       Steps to create Lambda Expression Java
                                                              i.      Create an Interface with abstract method having the same signature of the lambda function and use it as the type for lambda expression variable. This interface must have only one method.
                                                            ii.      To run this action, call method in the interface.
                                                          iii.      Its just like implementation of singe function.
public interface LambdaType {
     void foo();
public static void main(String[] args) {   
LambdaType myLambmadaVoidNoParam = ()->{System.out.println("HIII");};
myLambmadaVoidNoParam.foo();     
   }
d.      Lambda Examples
e.      Why lambda expressions ?
                                                              i.      We are using lambda expression to enable functional programming in java.
                                                            ii.      To write more readable, maintainable and clean less code.
                                                          iii.      To use apis very easily effectively.
                                                           iv.      To enable processing also
4.     Function Interface
a.      Java.util.function has special interface like bleow, it contains generic methods used as type for lambda expression wirh same type and signature.
                                                              i.      Predecate Interface
1.      It is already having boolean method. So whenever we need a lambda expression with Boolean return type use Predicate as  type. No nned to create another interface.
2.      Lot of other interfaces available like this.

5.     Streams
List.stream().filter(student->getName().startWith(“C”)).foreach(p->System.out.println(p.getName())).

a.      Stream consists of 3 main elements
                                                              i.      Source
1.      Collection
                                                            ii.      Operations
1.      Filter(), used to give condition
                                                          iii.      end operations
1.      count()
b.      More succinct code, sometimes just one liner
c.       You can pass lambda expression, which gives you the immense flexibility to change what you do in the loop.
d.      .parallel() can be used for parallel operations.
e.       ForEach looping can be made parallel with minimal effort e.g. without writing a single line of concurrent code, all you need to do is call parallelStream() method. 
f.         forEach() method is defined at two places, on Iterable interface as well as on Stream class. which means list.forEach() and list.stream.forEach() both are valid.
g.      Prefer using forEach() with streams because streams are lazy and not evaluated until a terminal operation is called. 
h.      forEach() is a terminal operation, you cannot call any method on stream after this.
i.        When you run forEach() method on parallel stream the order on which elements are processed is not guaranteed, though you can use forEachOrdered() to impose ordering.
j.        forEach() method accepts a Consumer instance, which is a functional interface, that's why you can pass a lambda expression to it. 
6.     PUT vs POST
a.      PUT implies putting a resource - completely replacing whatever is available at the given URL with a different thing. By definition, a PUT is idempotent. Do it as many times as you like, and the result is the same. x=5 is idempotent. You can PUT a resource whether it previously exists, or not (eg, to Create, or to Update)! 
b.      POST updates a resource, adds a subsidiary resource, or causes a change. A POST is not idempotent, in the way that x++ is not idempotent.
c.       PATCH: Submits a partial modification to a resource. If you only need to update one field for the resource, you may want to use the PATCH method.
7.     JBOSS deployment
a.      JBoss AS 7: Copy the .war file to JBOSS_HOME/standalone/deployments.
b.      Actually, for the latest JBOSS 7 AS, we need a .deeploy marker even for archives. So add a marker to trigger the deployment. 
c.        To start server:
> cd bin
              > standalone.bat
      start tomcat
         cd bin
         startup.bat

8.     1.6 to 1.8 migration
a.      upgraded from Java 6 to Java 8, to benefit from improvements in speed, brevity of code, and lower memory usage.
b.      The JDK is backwards compatible, meaning that JDK 8 can run binaries produced by JDK 7 and JDK 6. The javac compiler can also produce binaries targeting earlier versions of the JDK. In planning to upgrade, we will use this capability.
c.       When upgrading infrastructure, it is important to segment the architecture. Rather than upgrading everything at the same time, separate it into different environments so that you can test each one on its own.
d.      Environment Variables for installation
                                                              i.      PATH – identifies which actual java executable your system will use when you call java. You can circumvent this by explicitly calling the executable via /opt/jdk8/bin/java or /opt/jdk7/bin/java commands. Just call the one you want to use at the time. If you use scripts that rely on environment variables, consider isolating your terminals if you change the environment.
                                                            ii.      JAVA_HOME – some applications use this variable to identify where Java is.
                                                          iii.      Test your upgrade the following commands:
1.      java -version
2.      This should reply back with 1.8 or 1.7 depending on which you want. Just make sure it is right

3.      echo $JAVA_HOME

No comments:

Post a Comment

Search This Blog

Contact us

Name

Email *

Message *