Operator :: (double colon) in Java 8: complete guide with examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Operator :: (Double Colon) in Java 8: from beginner to pro

\n\n

Have you ever written code in Java 8 and seen a strange construct like System.out::println or String::length? Looks like magic, doesn't it? In fact, this is not magic, but one of the most elegant tools of modern Java — the double colon operator (also known as the double colon operator or method reference).

\n\n

In this article, we will break down the :: operator from the very basics to advanced techniques. You will learn to write cleaner, more concise, and more readable code. Let's go!

\n\n

What is the :: operator and why is it needed?

\n\n

The :: operator (double colon) was introduced in Java 8 as syntactic sugar for lambda expressions. If a lambda expression simply calls an existing method, you can replace it with a method reference. This makes the code shorter and more expressive.

\n\n

Compare for yourself:

\n\n
// Lambda expression\nlist.forEach(s -> System.out.println(s));\n\n// Method reference (:: operator)\nlist.forEach(System.out::println);
\n\n

The second option is not only shorter but also immediately says: "For each element, call the println method on System.out." Beautiful!

\n\n

Four types of method references

\n\n

In Java 8, there are four main types of method reference. Let's break down each with examples.

\n\n

1. Reference to a static method

\n\n

Syntax: ClassName::staticMethodName

\n\n

Example: counting even numbers in a list.

\n\n
import java.util.Arrays;\nimport java.util.List;\n\npublic class StaticMethodReference {\n    public static boolean isEven(Integer number) {\n        return number % 2 == 0;\n    }\n\n    public static void main(String[] args) {\n        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);\n        \n        // Lambda\n        long countLambda = numbers.stream()\n                .filter(n -> StaticMethodReference.isEven(n))\n                .count();\n        \n        // Reference to static method\n        long countRef = numbers.stream()\n                .filter(StaticMethodReference::isEven)\n                .count();\n        \n        System.out.println("Even numbers: " + countRef); // 3\n    }\n}
\n\n

Note: we are not calling the isEven method — we are simply passing a reference to it. Java will automatically substitute each element of the list as an argument.

\n\n

2. Reference to an instance method of a particular object

\n\n

Syntax: objectReference::instanceMethodName

\n\n

This type is used when you already have a specific object and want to call its method.

\n\n
import java.util.Arrays;\nimport java.util.List;\n\npublic class InstanceMethodReference {\n    public static void main(String[] args) {\n        List<String> names = Arrays.asList("Anna", "Boris", "Victor");\n        \n        // Create a StringBuilder object\n        StringBuilder sb = new StringBuilder("Names: ");\n        \n        // Lambda\n        names.forEach(name -> sb.append(name).append(" "));\n        \n        // Reference to instance method\n        // (but append returns StringBuilder, so we use lambda)\n        // For System.out.println - perfect:\n        names.forEach(System.out::println);\n        \n        System.out.println(sb.toString()); // Names: Anna Boris Victor\n    }\n}
\n\n

A classic example is System.out::println. System.out is a specific PrintStream object, and println is its method.

\n\n

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

\n\n

Syntax: ClassName::instanceMethodName

\n\n

This type is often confused with the previous one. Here we do not specify a specific object — instead, we say: "Take any object of this class and call its method."

\n\n


                
                

Blogs

Book Recommendations