Open In App

Double colon (::) operator in Java

Last Updated : 23 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
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
    // 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
    
  • Using double colon operator:
    stream.forEach( System.out::println);
    Program: To demonstrate the use of double colon operator Java
    // 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
    
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: Java
    // 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
    
  2. Instance method Syntax:
    (objectOfClass::methodName)
    Example:
    System.out::println
    Program: Java
    // 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
    
  3. Super method Syntax:
    (super::methodName)
    Example:
    super::someSuperClassMethod
    Program: Java
    // 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
    
  4. Instance method of an arbitrary object of a particular type Syntax:
    (ClassName::methodName)
    Example:
    SomeClass::someInstanceMethod
    Program: Java
    // 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
    
  5. Class Constructor Syntax:
    (ClassName::new)
    Example:
    ArrayList::new
    Program: Java
    // 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
    

Next Article
Article Tags :
Practice Tags :

Similar Reads