Double colon (::) operator in Java

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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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));
        }
    }

    chevron_right

    
    

    Output:

    Geeks
    For
    Geeks
    A
    Computer
    Portal
    
  • Using double colon operator:
    stream.forEach( System.out::println(s));

    Program: To demonstrate the use of double colon operator

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    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:

  1. Static method

    Syntax:

    (ClassName::methodName)

    Example:

    SomeClass::someStaticMethod

    Program:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    Geeks
    For
    GEEKS
    
  2. Instance method

    Syntax:

    (objectOfClass::methodName)

    Example:

    System.out::println

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    Geeks
    For
    GEEKS
    
  3. Super method

    Syntax:

    (super::methodName)

    Example:

    super::someSuperClassMethod

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    Hello Geeks
    Bye Geeks
    
    Hello For
    Bye For
    
    Hello GEEKS
    Bye GEEKS
    
  4. Instance method of an arbitrary object of a particular type

    Syntax:



    (ClassName::methodName)

    Example:

    SomeClass::someInstanceMethod

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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); 
        
    }

    chevron_right

    
    

    Output:

    Geeks
    For
    GEEKS
    
  5. Class Constructor

    Syntax:

    (ClassName::new)

    Example:

    ArrayList::new

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    Hello Geeks
    Hello For
    Hello GEEKS
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Peipei Wang



Article Tags :
Practice Tags :


6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.