Monday, April 15, 2019

Lambda Expression

 LAMBDA EXPRESSION



  • Lambda Expression is the one of the most important and biggest features in the jdk 8. It provides the clear and concise way to represent one method interface using an expression. The lambda expression is used to provide the implementation of an interface which has functional interface. it also helps to simplified the development alot. If you look at history and try to find out any language improvement in java in last 2 decades, you will not be able to recall many exciting things. Only few concurrent classes, generics and if you are agree then annotations as well, are remarkable additions in java in last decade. Lambda expression break this drought and feels like a pleasant gift.
        
In this Blog post, ill going to cover lambda expression and the basic concepts examples.                                                                                                                      
                    👉 Lambda expression small introduction in java
                           
                         👉 Java 8 functional interface

                         👉 Lambda expression examples.



  Lambda Expression small introduction in java

  • As I said earlier, it is an special features in java 8. and most welcoming by everyone. But the question is "Well, it is the function without the name and identifier. that's it But why everyone is so excited????? "😲🤔🤔🤔

  • The answer lies in the benefits involves in functional programming over oop(Object-Oriented Programming). Most OOP languages evolve around objects and instances and treat only them their first class citizens. this is specially true in java, where function can't  exist outside an object. a function itself does not mean anything in java, until it related to some object or instances.


  •   But in functional programming , you can define functions, give them reference  variables and pass them as method arguments and much more. JavaScript is a good  example of this where you can pass callback methods. its very useful feature and has been lacking in java from previous decades. Now in jdk 8, we can also use these things.
           
    

Syntax :- (a,b) - >  a+b ;  // This function has two parameters which are a and b. and it returns sum of these both.


Next, Have a look about Java 8 functional Interface.

2. Functional Interface in java 8


  • Single Abstract method Interface is not a new concept. it means interfaces with only one single method. We already had many examples. from jdk 8 , they will also be referred as functional interfaces as well. jdk 8 , enforces the rule of single responsibility by making these interfaces with  new annotation :- @FunctionalInterface .
     There is a good and well known example us Runnable Interface in thread. 



If i try to add a new method in any functional interface, compiler would not allow us to do this and will throw compile time error. 




So far good. But how they related to Lambda Expressions? 🤔
 Lets find 🏃

We know that Lambda expression is anonymous function with no name and they are passed to functions as parameters. Well in java methods parameters always have a type and this type information is looked for to determine which method needs to be called in case method overloading or even simple method calling. So normally, Every lambda expression also must be convertible to some type to be accepted as method parameters. Well that type in which lambda expressions are converted , are always of functional interface type. 


Example:- 



After use the Lambda Expression, The code will be: 



We can also see that Runnable is a functional Interface which does not allow more than one method . So when you pass lambda express to constructor of Thread class , compiler tries to convert the expression equivalent code as shown in first code sample,  If compiler succeed then everything runs fine, if compiler is not able to convert expression into equivalent implementation code, it will complain. 








3. Java 8 Lambda Expression Examples



1. Iterating over a List and perform some operations




2. Create a new runnable and pass it to thread



3. Adding an event listener to a GUI component



Above are very basic examples of lambda expression in java 8.

In the conclusion, I think this post might give the basic knowledge and examples of lambda expression to construct. Feel free to share your feedback and comments below.

Thank you for being here and keep reading..... 😃👍



Wednesday, April 10, 2019

Discussed about Some Features in previous Java Versions (7,8,9 and 10)

JAVA!!! 💭💻

Yes we all heard this term at least once  in several places in our developing environment.



Java is platform independent object oriented programming language and its main objective is write once run anywhere. It is a general-purpose programming language, it can be used to create all kinds of programs. It is used to developing mobile, web applications, server side and dynamic applications. java is a most populate and powerful language in computer programming. As a developer, just coding the applications is no longer helpful but also we should aware about the languages and had good knowledge is more and more useful in our career. In java we should know about the changes in the different versions which come across and made a good impact in Developments. So i'm going to share some knowledge about the changes in the some important java versions in recent times. Lets Start!!! 🏃


Java Version 


Evaluation of the java affects more impact in developments nowadays. Java language has undergone several changes since JDK 1.0 as well as numerous additions of  classes and packages to the standard library. There is much more dramatic changes have been made to the Java Class Library , over the years. 


here we are going to discuss about the features of the JDK 7,8,9 and 10.


JDK7 FEATURES


This is the major release after Oracle acquires Sun Micro Systems. It was launched on 7th of July 2011 but available for developers on July 2011. some additional features will added in this version.


1.In the JDK 7,We can able to use String datatype in Switch Statements.
           
  Example:- 
                       
                 




   2.   Single catch statement for throwing multiple exception by using " | "  operator.
   
         Example:-
      
           Prior to Java 7
                   
                      
          In java 7

             
    

3.  In Java 7 , we can use Underscore for Numeric Literals.


    Example:

           


But still some limitation are here:
  • Adjacent to a decimal point in a floating point
  • At the Beginning or end of a number.

  

4. No need close() Resource
  
 Resources like Input/Out Streams, Files, Connections should be closed manually by developer.Usually we use a try-finally block to close the respective resources. However, Java 7 has introduced another cool feature to manage the resources automatically. any object that implements java.lang.AutoClosable,which includes all objects which implement java.io.Closable, can be used as a resource. 

Prior to java 7






 In Java 7 


   



JDK8 FEATURES


Now that we have some idea about Java 7 and its new features . It was released on March 18, 2014. lets talk about what java 8 brought 🔎


1. Functional Interface
  
Functional Interfaces are new concept which is introduced by Java 8.Interface which contains only single method called as Functional Interface. in other-words, an interface which has exactly one abstract method becomes functional Interface. 

   Example:
   
       public Interface Test{
         
            public void doCalc(int i);
       }
  




2. Lambda Expression

It is an  another important and interesting feature which is introduced by Java 8. We can use Lambda Expression instead of implementing Anonymous class of an Interface that contains only one method. 

Prior to Java 8



In Java 8

Ill Explain more information about these Functional Interface and Lambda Expression in my upcoming posts. 👍


3. Default Method in Interface

  An interface is an 100% abstract class. It could have only abstract methods. The implementation of these methods has to be provided in a separate class. To overcome this problem Java 8 Introduced the concept of default methods which allows the interfaces to are implementation.  


       

     



Here the child class no need to override the method2() method but still we can call it. 





4. Static Default Method in Interface
   
  we can also have Static default method in interface which is a java 8 features. 





 Output is 








6. For-each Method

 ForEach library function is new in java 8 and it has made developers life Quicker and Easier. Collection classes which extends Iterable interface can use forEach loop to iterate elements.

Example:- 







JDK9 FEATURES


More than 3 years after the release of java 8, the next version java 9 released on September 21st of 2017. 


1. Private Interface Methods


  • Java 8 brought us default methods and static methods on interfaces. 
  • we can create private  method inside an interface. Interface allows us to declare private methods that help to share common code between non-abstract methods.

       Before Java 9 , Creating private methods inside an interface cause a compile time error. 



This is written JDK8 Version
Here , java 8 doesn't support the private method in interface.


                                             JDK 9 Code




2. Jshell


  • It is an interactive Java shell Tool, it allows us to execute Java code from the shell and displays  output immediately. 
  • Jshell is a Read Evaluate Print Loo tool and run from command line. it is good  for the test the business Logic.





JDK 10 FEATURES




 1. Local Variable Type References

Java now has now "var" style declarations. it allows you to declare a local variable without specifying its datatype.






2. Garbage Collector Interface

  • In earlier JDK Structure, the components that made up a Garbage Collector(GC) implementation were scattered throughout various parts of the code base
  • Its changed in Java 10,Now it is clean interface within the JVM source code to allow alternative collectors to be quickly and easily integrated.



Closing Up


I touched some of the Features in JDK 7,8,9 and 10. Hope you found this post useful and interesting. Feel free to to share your feedback and comments Below.   


Thank you for being here and keep reading...
😃


Webservices — C# 👩🏻‍💻

  Webservices — C# 👩🏻‍💻 There are a lot of definitions defined about web services. In simple terms, The Web service is any software syste...