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:
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Stream<String> stream
= Stream.of( "Geeks" , "For" ,
"Geeks" , "A" ,
"Computer" ,
"Portal" );
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
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Stream<String> stream
= Stream.of( "Geeks" , "For" ,
"Geeks" , "A" ,
"Computer" ,
"Portal" );
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:
- Static method
Syntax:
(ClassName::methodName)
Example:
SomeClass::someStaticMethod
Program:
import java.util.*;
class GFG {
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" );
list.forEach(GFG::someFunction);
}
}
|
- Instance method
Syntax:
(objectOfClass::methodName)
Example:
System.out::println
Program:
import java.util.*;
class GFG {
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" );
list.forEach(( new GFG())::someFunction);
}
}
|
- Super method
Syntax:
(super::methodName)
Example:
super::someSuperClassMethod
Program:
import java.util.*;
import java.util.function.*;
class Test {
String print(String str)
{
return ( "Hello " + str + "\n" );
}
}
class GFG extends Test {
@Override
String print(String s)
{
Function<String, String>
func = super ::print;
String newValue = func.apply(s);
newValue += "Bye " + s + "\n" ;
System.out.println(newValue);
return newValue;
}
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add( "Geeks" );
list.add( "For" );
list.add( "GEEKS" );
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:
import java.util.*;
class Test {
String str= null ;
Test(String s)
{
this .str=s;
}
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" ));
list.forEach(Test::someFunction);
}
}
|
- Class Constructor
Syntax:
(ClassName::new)
Example:
ArrayList::new
Program:
import java.util.*;
class GFG {
public GFG(String s)
{
System.out.println( "Hello " + s);
}
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add( "Geeks" );
list.add( "For" );
list.add( "GEEKS" );
list.forEach(GFG:: new );
}
}
|
Output:
Hello Geeks
Hello For
Hello GEEKS