Double colon (::) operator in Java

23 Feb, 2022
The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions. The only difference it has from lambda expressions is that this uses direct reference to the method by name instead of providing a delegate to the method.
Syntax:
<Class name>::<method name>
Example: To print all elements of the stream:
Using Lambda expression:stream.forEach( s-> System.out.println(s));Program:// Java code to print the elements of Stream// without using double colon operator  import java.util.stream.*;  class GFG {    public static void main(String[] args)    {          // Get the stream        Stream<String> stream            = Stream.of("Geeks", "For",                        "Geeks", "A",                        "Computer",                        "Portal");          // Print the stream        stream.forEach(s -> System.out.println(s));    }}Output:Geeks
For
Geeks
A
Computer
Portal

stream.forEach( s-> System.out.println(s));
Program:
// Java code to print the elements of Stream// without using double colon operator  import java.util.stream.*;  class GFG {    public static void main(String[] args)    {          // Get the stream        Stream<String> stream            = Stream.of("Geeks", "For",                        "Geeks", "A",                        "Computer",                        "Portal");          // Print the stream        stream.forEach(s -> System.out.println(s));    }}
Geeks
For
Geeks
A
Computer
Portal

Using double colon operator:stream.forEach( System.out::println);Program: To demonstrate the use of double colon operator// Java code to print the elements of Stream// using double colon operator  import java.util.stream.*;  class GFG {    public static void main(String[] args)    {          // Get the stream        Stream<String> stream            = Stream.of("Geeks", "For",                        "Geeks", "A",                        "Computer",                        "Portal");          // Print the stream        // using double colon operator        stream.forEach(System.out::println);    }}Output:Geeks
For
Geeks
A
Computer
Portal

stream.forEach( System.out::println);
Program: To demonstrate the use of double colon operator
// Java code to print the elements of Stream// using double colon operator  import java.util.stream.*;  class GFG {    public static void main(String[] args)    {          // Get the stream        Stream<String> stream            = Stream.of("Geeks", "For",                        "Geeks", "A",                        "Computer",                        "Portal");          // Print the stream        // using double colon operator        stream.forEach(System.out::println);    }}
Geeks
For
Geeks
A
Computer
Portal

When and how to use double colon operator?
Method reference or double colon operator can be used to refer:
a static method,
an instance method, or
a constructor.
How to use method reference in Java:
Static methodSyntax:(ClassName::methodName)Example:SomeClass::someStaticMethodProgram:// Java code to show use of double colon operator// for static methods  import java.util.*;  class GFG {      // static function to be called    static void someFunction(String s)    {        System.out.println(s);    }      public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the static method        // using double colon operator        list.forEach(GFG::someFunction);    }}Output:Geeks
For
GEEKS
Instance methodSyntax:(objectOfClass::methodName)Example:System.out::printlnProgram:// Java code to show use of double colon operator// for instance methods  import java.util.*;  class GFG {      // instance function to be called    void someFunction(String s)    {        System.out.println(s);    }      public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the instance method        // using double colon operator        list.forEach((new GFG())::someFunction);    }}Output:Geeks
For
GEEKS
Super methodSyntax:(super::methodName)Example:super::someSuperClassMethodProgram:// Java code to show use of double colon operator// for super methods  import java.util.*;import java.util.function.*;  class Test {      // super function to be called    String print(String str)    {        return ("Hello " + str + "\n");    }}  class GFG extends Test {      // instance method to override super method    @Override    String print(String s)    {          // call the super method        // using double colon operator        Function<String, String>            func = super::print;          String newValue = func.apply(s);        newValue += "Bye " + s + "\n";        System.out.println(newValue);          return newValue;    }      // Driver code    public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the instance method        // using double colon operator        list.forEach(new GFG()::print);    }}Output:Hello Geeks
Bye Geeks

Hello For
Bye For

Hello GEEKS
Bye GEEKS
Instance method of an arbitrary object of a particular typeSyntax:(ClassName::methodName)Example:SomeClass::someInstanceMethodProgram:// Java code to show use of double colon operator // for instance method of arbitrary type   import java.util.*;   class Test {     String str=null;      Test(String s)    {        this.str=s;    }    // instance function to be called     void someFunction()     {         System.out.println(this.str);     } }   class GFG {       public static void main(String[] args)     {           List<Test> list = new ArrayList<Test>();         list.add(new Test("Geeks"));         list.add(new Test("For"));         list.add(new Test("GEEKS"));           // call the instance method         // using double colon operator         list.forEach(Test::someFunction);     } }Output:Geeks
For
GEEKS
Class ConstructorSyntax:(ClassName::new)Example:ArrayList::newProgram:// Java code to show use of double colon operator// for class constructor  import java.util.*;  class GFG {      // Class constructor    public GFG(String s)    {        System.out.println("Hello " + s);    }      // Driver code    public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the class constructor        // using double colon operator        list.forEach(GFG::new);    }}Output:Hello Geeks
Hello For
Hello GEEKS

Static methodSyntax:(ClassName::methodName)Example:SomeClass::someStaticMethodProgram:// Java code to show use of double colon operator// for static methods  import java.util.*;  class GFG {      // static function to be called    static void someFunction(String s)    {        System.out.println(s);    }      public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the static method        // using double colon operator        list.forEach(GFG::someFunction);    }}Output:Geeks
For
GEEKS

Syntax:
(ClassName::methodName)
Example:
SomeClass::someStaticMethod
Program:
// Java code to show use of double colon operator// for static methods  import java.util.*;  class GFG {      // static function to be called    static void someFunction(String s)    {        System.out.println(s);    }      public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the static method        // using double colon operator        list.forEach(GFG::someFunction);    }}
Geeks
For
GEEKS

Instance methodSyntax:(objectOfClass::methodName)Example:System.out::printlnProgram:// Java code to show use of double colon operator// for instance methods  import java.util.*;  class GFG {      // instance function to be called    void someFunction(String s)    {        System.out.println(s);    }      public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the instance method        // using double colon operator        list.forEach((new GFG())::someFunction);    }}Output:Geeks
For
GEEKS

Syntax:
(objectOfClass::methodName)
Example:
System.out::println
Program:
// Java code to show use of double colon operator// for instance methods  import java.util.*;  class GFG {      // instance function to be called    void someFunction(String s)    {        System.out.println(s);    }      public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the instance method        // using double colon operator        list.forEach((new GFG())::someFunction);    }}
Geeks
For
GEEKS

Super methodSyntax:(super::methodName)Example:super::someSuperClassMethodProgram:// Java code to show use of double colon operator// for super methods  import java.util.*;import java.util.function.*;  class Test {      // super function to be called    String print(String str)    {        return ("Hello " + str + "\n");    }}  class GFG extends Test {      // instance method to override super method    @Override    String print(String s)    {          // call the super method        // using double colon operator        Function<String, String>            func = super::print;          String newValue = func.apply(s);        newValue += "Bye " + s + "\n";        System.out.println(newValue);          return newValue;    }      // Driver code    public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the instance method        // using double colon operator        list.forEach(new GFG()::print);    }}Output:Hello Geeks
Bye Geeks

Hello For
Bye For

Hello GEEKS
Bye GEEKS

Syntax:
(super::methodName)
Example:
super::someSuperClassMethod
Program:
// Java code to show use of double colon operator// for super methods  import java.util.*;import java.util.function.*;  class Test {      // super function to be called    String print(String str)    {        return ("Hello " + str + "\n");    }}  class GFG extends Test {      // instance method to override super method    @Override    String print(String s)    {          // call the super method        // using double colon operator        Function<String, String>            func = super::print;          String newValue = func.apply(s);        newValue += "Bye " + s + "\n";        System.out.println(newValue);          return newValue;    }      // Driver code    public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the instance method        // using double colon operator        list.forEach(new GFG()::print);    }}
Hello Geeks
Bye Geeks

Hello For
Bye For

Hello GEEKS
Bye GEEKS

Instance method of an arbitrary object of a particular typeSyntax:(ClassName::methodName)Example:SomeClass::someInstanceMethodProgram:// Java code to show use of double colon operator // for instance method of arbitrary type   import java.util.*;   class Test {     String str=null;      Test(String s)    {        this.str=s;    }    // instance function to be called     void someFunction()     {         System.out.println(this.str);     } }   class GFG {       public static void main(String[] args)     {           List<Test> list = new ArrayList<Test>();         list.add(new Test("Geeks"));         list.add(new Test("For"));         list.add(new Test("GEEKS"));           // call the instance method         // using double colon operator         list.forEach(Test::someFunction);     } }Output:Geeks
For
GEEKS

Syntax:
(ClassName::methodName)
Example:
SomeClass::someInstanceMethod
Program:
// Java code to show use of double colon operator // for instance method of arbitrary type   import java.util.*;   class Test {     String str=null;      Test(String s)    {        this.str=s;    }    // instance function to be called     void someFunction()     {         System.out.println(this.str);     } }   class GFG {       public static void main(String[] args)     {           List<Test> list = new ArrayList<Test>();         list.add(new Test("Geeks"));         list.add(new Test("For"));         list.add(new Test("GEEKS"));           // call the instance method         // using double colon operator         list.forEach(Test::someFunction);     } }
Geeks
For
GEEKS

Class ConstructorSyntax:(ClassName::new)Example:ArrayList::newProgram:// Java code to show use of double colon operator// for class constructor  import java.util.*;  class GFG {      // Class constructor    public GFG(String s)    {        System.out.println("Hello " + s);    }      // Driver code    public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the class constructor        // using double colon operator        list.forEach(GFG::new);    }}Output:Hello Geeks
Hello For
Hello GEEKS

Syntax:
(ClassName::new)
Example:
ArrayList::new
Program:
// Java code to show use of double colon operator// for class constructor  import java.util.*;  class GFG {      // Class constructor    public GFG(String s)    {        System.out.println("Hello " + s);    }      // Driver code    public static void main(String[] args)    {          List<String> list = new ArrayList<String>();        list.add("Geeks");        list.add("For");        list.add("GEEKS");          // call the class constructor        // using double colon operator        list.forEach(GFG::new);    }}
Hello Geeks
Hello For
Hello GEEKS

Peipei Wang
richard20
Operators
Java
Java
Operators
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.