JAVA Features – Method Reference – Double Colon Operator in Java

This blog explains about Method reference double colon operator in java is given below : 

Method Reference:

:: Double Colon Operator:

Short cut for Lambda Expressions:
Code Readability:
Compact.

ArrayList<Integer> al = new ArrayList<Integer>(); 
al.add(10); 
al.add(20);
al.add(30); 
//al.forEach(value -> System.out.println(value));

al.forEach(System.out::println);
Functional Interface: 

@FunctionalInterface
public interface Contract {
	
	public void display(int no);

}

Arguments should match in interface method and our class method.

We can use Method Reference for static and non-static methods.

@FunctionalInterface
public interface Contract {
	
	public void display(int no);

}


public class Mobile {
	
	int price; 
	Mobile(int price)
	{
		System.out.println("Constructor "+ price);
	}
	
	public void show(int value)
	{
		System.out.println(value);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	//	Mobile m = new Mobile(123); 
		
//		Contract c = a -> System.out.println(a);
		Contract c = new Mobile(123)::show; 
		//c.display(123);
		
		c = Mobile::new; 
		c.display(10000);
		

	}