Double colon (::) operator in Java
Last Updated :
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:
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 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);
}
}
- 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);
}
}
- 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
- 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);
}
}
- 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
Similar Reads
dot(.) Operator in Java The dot (.) operator is one of the most frequently used operators in Java. It is essential for accessing members of classes and objects, such as methods, fields, and inner classes. This article provides an in-depth look at the dot operator, its uses, and its importance in Java programming.Dot(.) Ope
4 min read
Bitwise Operators in Java In Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming.There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level. T
6 min read
Basic Operators in Java Java provides a rich operator environment. We can classify the basic operators in java in the following groups: Arithmetic OperatorsRelational OperatorsBitwise OperatorsAssignment OperatorsLogical Operators Let us now learn about each of these operators in detail. 1. Arithmetic Operators: Arithmetic
10 min read
Compound Assignment Operators in Java In Java, compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on two operands before assigning the result to the first operand.The following are all possible assignment operators in Java:1. += (compound addit
6 min read
Java.Lang.Double Class in Java Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. Double class is a wrapper class for the pr
4 min read