IL operatore due punti (::). , conosciuto anche come operatore di riferimento del metodo in Java, viene utilizzato per chiamare un metodo facendo riferimento ad esso direttamente con l'aiuto della sua classe. Si comportano esattamente come le espressioni lambda. L'unica differenza rispetto alle espressioni lambda è che utilizza un riferimento diretto al metodo per nome invece di fornire un delegato al metodo.
Sintassi:
::>
Esempio: Per stampare tutti gli elementi dello stream:
- Utilizzando l'espressione Lambda:
stream.forEach( s->System.out.println(s));>
Programma:
// 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 stream>>= Stream.of(>'Geeks'>,>'For'>,>>'Geeks'>,>'A'>,>>'Computer'>,>>'Portal'>);>>>// Print the stream>>stream.forEach(s ->System.out.println(s));>>}>}>>
>
Produzione:Geeks For Geeks A Computer Portal>
- Utilizzando l'operatore dei due punti:
stream.forEach( System.out::println);>
Programma: Dimostrare l'uso dell'operatore dei due punti
// 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 stream>>= Stream.of(>'Geeks'>,>'For'>,>>'Geeks'>,>'A'>,>>'Computer'>,>>'Portal'>);>>>// Print the stream>>// using double colon operator>>stream.forEach(System.out::println);>>}>}>>
>
Produzione:Geeks For Geeks A Computer Portal>
Quando e come utilizzare l'operatore dei due punti?
Il riferimento al metodo o l'operatore dei due punti può essere utilizzato per fare riferimento a:
- un metodo statico,
- un metodo di istanza, o
- un costruttore.
Come utilizzare il riferimento al metodo in Java:
- Metodo statico
Sintassi:
(ClassName::methodName)>
Esempio:
SomeClass::someStaticMethod>
Programma:
// 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 list =>new>ArrayList();>>list.add(>'Geeks'>);>>list.add(>'For'>);>>list.add(>'GEEKS'>);>>>// call the static method>>// using double colon operator>>list.forEach(GFG::someFunction);>>}>}>>
>
Produzione:Geeks For GEEKS>
- Metodo di istanza
Sintassi:
(objectOfClass::methodName)>
Esempio:
System.out::println>
Programma:
// 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 list =>new>ArrayList();>>list.add(>'Geeks'>);>>list.add(>'For'>);>>list.add(>'GEEKS'>);>>>// call the instance method>>// using double colon operator>>list.forEach((>new>GFG())::someFunction);>>}>}>>
costruttore in Java
>
Produzione:Geeks For GEEKS>
- Metodo eccellente
Sintassi:
(super::methodName)>
Esempio:
super::someSuperClassMethod>
Programma:
// 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 +>' '>);>>}>}>>class>GFG>extends>Test {>>>// instance method to override super method>>@Override>>String print(String s)>>{>>>// call the super method>>// using double colon operator>>Function>>func =>super>::print;>>>String newValue = func.apply(s);>>newValue +=>'Bye '>+ s +>' '>;>>System.out.println(newValue);>>>return>newValue;>>}>>>// Driver code>>public>static>void>main(String[] args)>>{>>>List list =>new>ArrayList();>>list.add(>'Geeks'>);>>list.add(>'For'>);>>list.add(>'GEEKS'>);>>>// call the instance method>>// using double colon operator>>list.forEach(>new>GFG()::print);>>}>}>>
>
Produzione:Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
- Metodo di istanza di un oggetto arbitrario di un tipo particolare
Sintassi:
(ClassName::methodName)>
Esempio:
SomeClass::someInstanceMethod>
Programma:
// 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 list =>new>ArrayList();>>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);>>}>}>>
>
Produzione:Geeks For GEEKS>
- Costruttore di classi
Sintassi:
(ClassName::new)>
Esempio:
ArrayList::new>
Programma:
// 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 list =>new>ArrayList();>>list.add(>'Geeks'>);>>list.add(>'For'>);>>list.add(>'GEEKS'>);>>>// call the class constructor>>// using double colon operator>>list.forEach(GFG::>new>);>>}>}>>
>
Produzione:Hello Geeks Hello For Hello GEEKS>