Tuesday 27 June 2017

What is Constructor reference | Method reference in Java 8 | Java method reference


Click here to watch in Youtube :
https://www.youtube.com/watch?v=-42_MSVAWIg&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
What is Constructor reference | Method reference in Java 8 | Java method reference 
LambdaDemo.java
@FunctionalInterface
interface Message
{
    String displayMessage(char[] chArray);
}

public class LambdaDemo
{
    public static void main(String[] args)
    {

        /*
         * Note that, in this program strFunc() of FunctionalInterface
         * returns a reference of type String. In this program, the
         * expression "String::new" creates a constructor reference to
         * the String's constructor.
         */
        Message message = String::new;

        char[] charArray =  { 'P', 'e', 't', 'e', 'r' };

        /*
         * Here the method StrFunc() takes a Character array as an
         * argument, so the parameterized constructor 
         * String(char[] charArray) is referred here.
         * 
         * So finally, when ever you call the method strFunc() on
         * FunctionalInterface reference, a String object is
         * constructed and returned.
         */
        System.out.println(message.displayMessage(charArray));
    }

}
Output
Peter
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_Cons_ref_app.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LambdaDemo_Cons_ref_app

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/525432e9afa25ee5052810cb2318f64453ffeffa/BasicJava/LambdaDemo_Cons_ref_app/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment