Monday 12 June 2017

Static Method reference - BiFunction Overload methods | Method reference in Java 8


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

Click the below Image to Enlarge
Static Method reference - BiFunction Overload methods | Method reference in Java 8 
MethodReferenceDemo.java
import java.util.function.BiFunction;

/**
 *
 * We can also override static methods by referring methods. In the
 * following example, we have defined and overloaded three add
 * methods.
 *
 */

class Arithmetic
{
    public static int add(int a, int b)
    {
        return a + b;
    }

    public static float add(int a, float b)
    {
        return a + b;
    }

    public static float add(float a, float b)
    {
        return a + b;
    }
}

public class MethodReferenceDemo
{
    public static void main(String[] args)
    {
        BiFunction<Integer, Integer, Integer> adder1 = Arithmetic::add;
        BiFunction<Integer, Float, Float> adder2 = Arithmetic::add;
        BiFunction<Float, Float, Float> adder3 = Arithmetic::add;

        int result1 = adder1.apply(10, 20);
        System.out.println(result1);
        
        float result2 = adder2.apply(10, 20.3f);
        System.out.println(result2);
        
        float result3 = adder3.apply(10.6f, 20.8f);     
        System.out.println(result3);
    }
}
Output
30
30.3
31.4
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_method_ref_static_BiFunc_overload_app.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/525432e9afa25ee5052810cb2318f64453ffeffa/BasicJava/LambdaDemo_method_ref_static_BiFunc_overload_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