Attention: Java promotion camp, the latest article delivered at the first time, 10T free learning materials available at any time!!!
The double colon (::) operation, also known as the method reference operator, is used to directly call methods of a specified class. Its behavior is exactly the same as that of a la mbda expression. It and lambda expressions The only difference is that it references the method directly by name, rather than providing a delegate for the method.
Syntax:
<Class name>::<method name>
Example: to print all elements of a Stream:
- Use Lambda expressions:
stream.forEach(s-> System.out.println(s));
Complete example:
// 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
- Use the double colon operator:
stream.forEach(System.out::println(s));
Complete example:
// 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 do I use the double colon operator?
Method references or double colon operators are available for the following methods:
- Static method
- Example method
- Constructor
How to use method references in Java:
- Static method
Syntax:
(ClassName::methodName)
Example:
SomeClass::someStaticMethod
Complete example:
// 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
- Example method
Syntax:
(objectOfClass::methodName)
Example:
System.out::println
Complete example:
// 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
- Parent class method
Syntax:
(super :: methodName)
Example:
super::someSuperClassMethod
Complete example:
// 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 methods for specific types of objects
Syntax:
(ClassName::methodName)
Example:
SomeClass::someInstanceMethod
Complete example:
// 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 constructor
Syntax:
(ClassName::new)
Example:
ArrayList::new
Complete example:
// 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