Method Reference Example - LambdaMethodRefernce.java

This section provides a tutorial example on how to use method references.

In order to play with method references, let's try to convert the lambda expression example program, LambdaCalculator.java, to use method references. Here is my version,

/* LambdaMethodRefernce.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
class LambdaMethodRefernce {
   interface Operation {
      int operate(int a, int b);   
   }
   public static int add(int a, int b) {
      return a+b;
   }
   public static int subtract(int a, int b) {
      return a-b;
   }
   public static int multiply(int a, int b) {
      return a*b;
   }
   public int calculate(int a, int b, Operation o) {
      System.out.println(o.toString());
      return o.operate(a, b);
   }

   public static void main(String[] arg) {
      int x = 1;
      String s = "+";
      int y = 2;
      if (arg.length>0) x = Integer.parseInt(arg[0]);
      if (arg.length>1) s = arg[1];
      if (arg.length>2) y = Integer.parseInt(arg[2]);

      LambdaMethodRefernce c = new LambdaMethodRefernce();
      if (s.equals("+")) 
         System.out.println(x+s+y+" = "
            +c.calculate(x,y,LambdaMethodRefernce::add));
      else if (s.equals("-")) 
         System.out.println(x+s+y+" = "
            +c.calculate(x,y,LambdaMethodRefernce::subtract));
      else if (s.equals("*")) 
         System.out.println(x+s+y+" = "
            +c.calculate(x,y,LambdaMethodRefernce::multiply));
      else
         System.out.println(x+s+y+" = Undefined");
   }
}

Compile and run the sample program in JDK 1.8. You will get some results like these:

G:\herong>java LambdaMethodRefernce
LambdaMethodRefernce$$Lambda$1/518248@cf9800
1+2 = 3

G:\herong>java LambdaMethodRefernce 8 + 2
LambdaMethodRefernce$$Lambda$1/518248@cf9800
8+2 = 10

G:\herong>java LambdaMethodRefernce 8 - 2
LambdaMethodRefernce$$Lambda$1/518248@cf9800
8-2 = 6

G:\herong>java LambdaMethodRefernce 8 "*" 2
LambdaMethodRefernce$$Lambda$1/518248@cf9800
8*2 = 16

G:\herong>java LambdaMethodRefernce 8 / 2
8/2 = Undefined

The output confirms that method references are lambda expressions.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

Lambda Expressions and Method References

 What Is Lambda Expression?

 LambdaCalculator.java - Lambda Expression Example

 Lambda Expression Syntax Options

 Lambda Expression as Method Reference

Method Reference Example - LambdaMethodRefernce.java

 Lambda Expression Stream Pipeline Operations

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version