Double Colon Operator in Java 8

It is used as a method reference by replacing lambda expression.

Syntax is 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
2
3
4
5
6
7
8
9
10
11
12
13
14
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();
    }
}
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();
	}
}

Output

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.
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 the syntax
Classname::methodname if method is static
instanceVarName :: methodname if method is non static

Let’s analyze another example – sorting person class objects

Person.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
    }
 
}
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;
	}

}

PersonSort.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
 
    }
 
}
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

1
2
3
4
5
6
7
8
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();
    }
 
}
package com.kb.doublecolon;

import java.util.Comparator;

public class PersonSortByAge {

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

}

PersonSort.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
 
    }
 
}
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 i.e Double colon(::) operator.

See the below program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
 
    }
 
}
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

Karibasappa G C (KB)
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