Der Doppelpunkt-Operator (::). , auch bekannt als Methodenreferenzoperator Wird in Java verwendet, um eine Methode aufzurufen, indem mit Hilfe ihrer Klasse direkt auf sie verwiesen wird. Sie verhalten sich genau wie die Lambda-Ausdrücke. Der einzige Unterschied zu Lambda-Ausdrücken besteht darin, dass hierbei ein direkter Verweis auf die Methode anhand des Namens verwendet wird, anstatt einen Delegaten für die Methode bereitzustellen.
Syntax:
::>
Beispiel: So drucken Sie alle Elemente des Streams:
- Verwendung des Lambda-Ausdrucks:
stream.forEach( s->System.out.println(s));>
Programm:
// 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));>>}>}>>
>
Ausgabe:Geeks For Geeks A Computer Portal>
- Verwendung des Doppelpunkt-Operators:
stream.forEach( System.out::println);>
Programm: Um die Verwendung des Doppelpunkt-Operators zu demonstrieren
// 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);>>}>}>>
>
Ausgabe:Geeks For Geeks A Computer Portal>
Wann und wie wird der Doppelpunktoperator verwendet?
Methodenreferenzen oder Doppelpunktoperatoren können verwendet werden, um auf Folgendes zu verweisen:
- eine statische Methode,
- eine Instanzmethode, oder
- ein Konstrukteur.
So verwenden Sie Methodenreferenzen in Java:
- Statische Methode
Syntax:
(ClassName::methodName)>
Beispiel:
SomeClass::someStaticMethod>
Programm:
enthält Teilzeichenfolge 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 list =>new>ArrayList();>>list.add(>'Geeks'>);>>list.add(>'For'>);>>list.add(>'GEEKS'>);>>>// call the static method>>// using double colon operator>>list.forEach(GFG::someFunction);>>}>}>>
>
Ausgabe:Geeks For GEEKS>
- Instanzmethode
Syntax:
(objectOfClass::methodName)>
Beispiel:
System.out::println>
Programm:
// 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);>>}>}>boto3
>
>
Ausgabe:Geeks For GEEKS>
- Super Methode
Syntax:
(super::methodName)>
Beispiel:
super::someSuperClassMethod>
Programm:
// 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);>>}>}>>
>
Ausgabe:Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
- Instanzmethode eines beliebigen Objekts eines bestimmten Typs
Syntax:
(ClassName::methodName)>
Beispiel:
SomeClass::someInstanceMethod>
Programm:
// 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);>>}>}>>
>
Ausgabe:Geeks For GEEKS>
- Klassenkonstruktor
Syntax:
(ClassName::new)>
Beispiel:
ArrayList::new>
Programm:
// 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>);>>}>}>>
>
Ausgabe:Hello Geeks Hello For Hello GEEKS>