El operador de dos punts (::). , també conegut com operador de referència del mètode a Java, s'utilitza per cridar un mètode fent referència a ell amb l'ajuda de la seva classe directament. Es comporten exactament com les expressions lambda. L'única diferència que té amb les expressions lambda és que utilitza una referència directa al mètode pel nom en lloc de proporcionar un delegat al mètode.
Sintaxi:
::>
Exemple: Per imprimir tots els elements del flux:
- Utilitzant l'expressió Lambda:
stream.forEach( s->System.out.println(s));>
Programa:
dhanashree verma
// 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));>>>}> }>>
>
Sortida:Geeks For Geeks A Computer Portal>
- Utilitzant l'operador de dos punts:
stream.forEach( System.out::println);>
Programa: Per demostrar l'ús de l'operador de dos punts
// 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);>>}>}>>
>
Sortida:Geeks For Geeks A Computer Portal>
Quan i com utilitzar l'operador de dos punts?
La referència del mètode o l'operador de dos punts es pot utilitzar per fer referència:
- un mètode estàtic,
- un mètode d'instància, o
- un constructor.
Com utilitzar la referència del mètode a Java:
- Mètode estàtic
Sintaxi:
(ClassName::methodName)>
Exemple:
SomeClass::someStaticMethod>
Programa:
// 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);>>}>}>>
>
Sortida:Geeks For GEEKS>
- Mètode d'instància
Sintaxi:
(objectOfClass::methodName)>
Exemple:
System.out::println>
Programa:
Java no
com canviar la cadena a int
// 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);>>}>}>>
>
Sortida:Geeks For GEEKS>
- Super mètode
Sintaxi:
(super::methodName)>
Exemple:
super::someSuperClassMethod>
Programa:
// 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);>>}>}>>
>
Sortida:Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
- Mètode d'instància d'un objecte arbitrari d'un tipus particular
Sintaxi:
(ClassName::methodName)>
Exemple:
SomeClass::someInstanceMethod>
Programa:
// 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);>>}>}>>
>
Sortida:Geeks For GEEKS>
- Constructor de classes
Sintaxi:
(ClassName::new)>
Exemple:
ArrayList::new>
Programa:
// 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>);>>}>}>>
>
c++ converteix int en cadena
Sortida:Hello Geeks Hello For Hello GEEKS>