Wednesday, 21 February 2018

Method Reference Example in Java 8 ~ foundjava

Java 8 New Feature with Method Reference

Java 8 Method Reference Example (foundjava)

we are going to see important and basic method reference example in java 8 tutorial.

Java 8 method reference is used to refer method of functional interface and to make program or code simple or clear you can use method reference instead of lambda expression.

It is shorthand notation of lambda expression to call any method.

'::' operator is used for method reference in java programs.

Before starting java 8 method reference example, let's see some types of method references one-by-one.


Types of Method Reference 

There are some java referece types of method reference which is given below with syntax.

1) Reference to an instance method 

syntax:

object :: instanceMethod

2) Reference to a static method

syntax:

Class :: staticMethod

3) Reference to an instance method of an arbitrary object of a particular type.

syntax:

Class :: instanceMethod

4) Reference to a constructor

syntax:

Class :: new


Let's understand above all the given java reference types with simple examples one-by-one.



(1) Reference to an instance Method

In this java 8 method reference example, We will create functional interface first and then a simple class with instance method and then we will use our method referece concept to refere to an instance method of an object.

@FunctionalInterface
interface Demo
{
void show();
}

public class Test
{
public void display()
{
System.out.println("I am instance method");
}
public static void main(String args[])
{
Test t = new Test();
Demo d = t :: display;//performing method reference using object
d.show();//calling method of function interface
}
}

Output: I am instance method


(2) Reference to a static method

interface Demo1
{
void show();
}

class Test1
{
public static void display()
{
System.out.println("Hi, i am static method of a class");
}
public static void main(String args[])
{
Demo1 dd = Test1 :: display;//perfroming method reference using class name
dd.show();
}
}

Output: Hi, i am static method of a class

Let' take another java method reference example


(3) Reference to an instance method of an arbitrary object of a particular type

This is another example of method reference to an instance method of an arbitray object of a particular type.

import java.util.Arrays;
class Test
{
public static void main(String args[])
{
String str[] = {"pink", "orange", "black", "red"};
Arrays.sort(str, String :: compareToIgnoreCase);

for(String str1 : str)
{
System.out.println(str1);
}
}
}

Output: black
             orange
             pink
             red


(4) Reference to a constructor

interface Demo4
{
FullName show(String s);
}

class FullName
{
FullName(String s)
{
System.out.println(s);
}
}

class Test
{
public static void main(String args[])
{
Demo4 d = FullName :: new;//performing constructor reference
d.show("Anurag Singh");
}
}

Output: Anurag Singh

In this java 8 tutorial, we saw 4 types of method references and some java 8 method reference examples step-by-step. I hope this java 8 new feature post will be helpful to you.

No comments:

Post a Comment