Double Colon Operator in Java 8

Double Colon Operator is used as a method reference by replacing lambda expression.

Syntax :

  1. className::methodName()
className::methodName()


Its applicable only in the place where lambda is applicable(means only for functional interface implementaions).

Let’s analyze with the below program

  1. package com.kb.doublecolon;
  2.  
  3. public class RunnableEx1 {
  4.  
  5.     static void show(){
  6.         System.out.println("show defined in separate method");
  7.     }
  8.    
  9.     public static void main(String[] args) {
  10.         Runnable r1 = () -> System.out.println("show defined in lambda body");
  11.         r1.run();
  12.  
  13.         Runnable r2 = RunnableEx1::show;
  14.         r2.run();
  15.     }
  16. }
package com.kb.doublecolon;

public class RunnableEx1 {

	static void show(){
		System.out.println("show defined in separate method");
	}
	
	public static void main(String[] args) {
		Runnable r1 = () -> System.out.println("show defined in lambda body");
		r1.run();

		Runnable r2 = RunnableEx1::show;
		r2.run();
	}
}

Runnable is a functional interface so it can be replaced by lambda expression as shown above with r1 declaraion

Now to replace this lambda expression , we can use one more feature of Java 8 and that is Double colon(::) operator.

So Double colon operator works well in all the places whenever we implement functional interface methods.

Steps to implement Double Colon(::) Operator


Step 1 :
Define a similar signature of functional interface method in a new class by providing body as required. Most important is signature should be same but method name can be changed.

Step 2 :
Replace lambda expression of implementing functional interface method and in that place add a method reference for a Method defined in class as specified in Step1

Using below syntax

  1. Classname::methodname    //if method is static
Classname::methodname    //if method is static
  1. instanceVarName :: methodname  //if method is non static
instanceVarName :: methodname  //if method is non static


Let’s analyze another example – sorting person class objects


Create Person.java

  1. package com.kb.doublecolon;
  2.  
  3. public class Person {
  4.     String name;
  5.     int age;
  6.     public String getName() {
  7.         return name;
  8.     }
  9.     public void setName(String name) {
  10.         this.name = name;
  11.     }
  12.     public int getAge() {
  13.         return age;
  14.     }
  15.     public void setAge(int age) {
  16.         this.age = age;
  17.     }
  18.     @Override
  19.     public String toString() {
  20.        
  21.         return "name = "+name+" age = "+age;
  22.     }
  23.  
  24. }
package com.kb.doublecolon;

public class Person {
	String name;
	int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		
		return "name = "+name+" age = "+age;
	}

}


Create PersonSort.java

  1. package com.kb.doublecolon;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. public class PersonSort {
  8.  
  9.     public static void main(String[] args) {
  10.         Person p1 = new Person();
  11.         Person p2 = new Person();
  12.         Person p3 = new Person();
  13.        
  14.         p1.setAge(20);
  15.         p1.setName("abc");
  16.        
  17.         p2.setAge(25);
  18.         p2.setName("xyz");
  19.        
  20.         p3.setAge(15);
  21.         p3.setName("def");
  22.        
  23.         List<Person> people = new ArrayList<Person>();
  24.         people.add(p1);
  25.         people.add(p2);
  26.         people.add(p3);
  27.         System.out.println("before sorting "+people);
  28.        
  29.         Collections.sort(people,(Person per1,Person per2) -> {
  30.             return per1.getAge() - per2.getAge();
  31.                     }
  32.         );
  33.        
  34.         System.out.println("after sorting "+people);
  35.  
  36.     }
  37.  
  38. }
package com.kb.doublecolon;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PersonSort {

	public static void main(String[] args) {
		Person p1 = new Person();
		Person p2 = new Person();
		Person p3 = new Person();
		
		p1.setAge(20);
		p1.setName("abc");
		
		p2.setAge(25);
		p2.setName("xyz");
		
		p3.setAge(15);
		p3.setName("def");
		
		List<Person> people = new ArrayList<Person>();
		people.add(p1);
		people.add(p2);
		people.add(p3);
		System.out.println("before sorting "+people);
		
		Collections.sort(people,(Person per1,Person per2) -> {
			return per1.getAge() - per2.getAge();
					}
		);
		
		System.out.println("after sorting "+people);

	}

}


Now I will add the sorting logic in one class and will call that logic in lambda body

Create PersonSortByAge.java

  1. package com.kb.doublecolon;
  2.  
  3. import java.util.Comparator;
  4.  
  5. public class PersonSortByAge {
  6.  
  7.     public int compare(Person p1,Person p2) {
  8.         return p1.getAge() - p2.getAge();
  9.     }
  10.  
  11. }
package com.kb.doublecolon;

import java.util.Comparator;

public class PersonSortByAge {

	public int compare(Person p1,Person p2) {
		return p1.getAge() - p2.getAge();
	}

}


Create PersonSort.java

  1. package com.kb.doublecolon;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. public class PersonSort {
  8.  
  9.     public static void main(String[] args) {
  10.         Person p1 = new Person();
  11.         Person p2 = new Person();
  12.         Person p3 = new Person();
  13.        
  14.         p1.setAge(20);
  15.         p1.setName("abc");
  16.        
  17.         p2.setAge(25);
  18.         p2.setName("xyz");
  19.        
  20.         p3.setAge(15);
  21.         p3.setName("def");
  22.        
  23.         List<Person> people = new ArrayList<Person>();
  24.         people.add(p1);
  25.         people.add(p2);
  26.         people.add(p3);
  27.         System.out.println("before sorting "+people);
  28.        
  29.         PersonSortByAge sortByAge = new PersonSortByAge();
  30.        
  31.         Collections.sort(people,(Person per1,Person per2) -> sortByAge.compare(per1, per2));
  32.        
  33.        
  34.         System.out.println("after sorting "+people);
  35.  
  36.     }
  37.  
  38. }
package com.kb.doublecolon;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PersonSort {

	public static void main(String[] args) {
		Person p1 = new Person();
		Person p2 = new Person();
		Person p3 = new Person();
		
		p1.setAge(20);
		p1.setName("abc");
		
		p2.setAge(25);
		p2.setName("xyz");
		
		p3.setAge(15);
		p3.setName("def");
		
		List<Person> people = new ArrayList<Person>();
		people.add(p1);
		people.add(p2);
		people.add(p3);
		System.out.println("before sorting "+people);
		
		PersonSortByAge sortByAge = new PersonSortByAge();
		
		Collections.sort(people,(Person per1,Person per2) -> sortByAge.compare(per1, per2));
		
		
		System.out.println("after sorting "+people);

	}

}


Now to replace this lambda expression calling method in its body, we can use one more feature of Java 8
That is Double colon (::) operator.

See the below program

  1. package com.kb.doublecolon;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. public class PersonSort {
  8.  
  9.     public static void main(String[] args) {
  10.         Person p1 = new Person();
  11.         Person p2 = new Person();
  12.         Person p3 = new Person();
  13.        
  14.         p1.setAge(20);
  15.         p1.setName("abc");
  16.        
  17.         p2.setAge(25);
  18.         p2.setName("xyz");
  19.        
  20.         p3.setAge(15);
  21.         p3.setName("def");
  22.        
  23.         List<Person> people = new ArrayList<Person>();
  24.         people.add(p1);
  25.         people.add(p2);
  26.         people.add(p3);
  27.         System.out.println("before sorting "+people);
  28.        
  29.         PersonSortByAge sortByAge = new PersonSortByAge();
  30.        
  31.         Collections.sort(people,sortByAge::compare);
  32.        
  33.        
  34.         System.out.println("after sorting "+people);
  35.  
  36.     }
  37.  
  38. }
package com.kb.doublecolon;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PersonSort {

	public static void main(String[] args) {
		Person p1 = new Person();
		Person p2 = new Person();
		Person p3 = new Person();
		
		p1.setAge(20);
		p1.setName("abc");
		
		p2.setAge(25);
		p2.setName("xyz");
		
		p3.setAge(15);
		p3.setName("def");
		
		List<Person> people = new ArrayList<Person>();
		people.add(p1);
		people.add(p2);
		people.add(p3);
		System.out.println("before sorting "+people);
		
		PersonSortByAge sortByAge = new PersonSortByAge();
		
		Collections.sort(people,sortByAge::compare);
		
		
		System.out.println("after sorting "+people);

	}

}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on