Operator dubbele dubbele punt (::) in Java

De operator met dubbele dubbele punt (::). , ook gekend als methodereferentieoperator in Java wordt het gebruikt om een ​​methode aan te roepen door er rechtstreeks naar te verwijzen met behulp van de klasse ervan. Ze gedragen zich precies zoals de lambda-uitdrukkingen. Het enige verschil met lambda-expressies is dat hierbij gebruik wordt gemaakt van een directe verwijzing naar de methode op naam in plaats van dat er een afgevaardigde voor de methode wordt opgegeven.

Syntaxis:

 :: 

Voorbeeld: Om alle elementen van de stream af te drukken:

  • Lambda-expressie gebruiken:
    stream.forEach( s->Systeem.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 ->Systeem.out.println(s));> > }> }>

    Uitgang:

     Geeks For Geeks A Computer Portal 
  • Met behulp van de dubbele dubbele punt-operator:
    stream.forEach( System.out::println); 

    Programma: Om het gebruik van de dubbele dubbelepuntoperator te demonstreren




    // 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);> > }> }>

    Uitgang:

     Geeks For Geeks A Computer Portal 

Wanneer en hoe gebruik ik de dubbele dubbele punt-operator?

Methodereferentie of dubbele dubbele punt-operator kan worden gebruikt om te verwijzen:

  • een statische methode,
  • een instantiemethode, of
  • een constructeur.

Methodereferentie gebruiken in Java:

  1. Statische methode

    Syntaxis:

    (ClassName::methodName) 

    Voorbeeld:

    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);> > }> }>

    Uitgang:

     Geeks For GEEKS 
  2. Instantiemethode

    Syntaxis:

    (objectOfClass::methodName) 

    Voorbeeld:

    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);> > }> }>

    Uitgang:

     Geeks For GEEKS 
  3. Supermethode

    Syntaxis:

    (super::methodName) 

    Voorbeeld:

    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);> > }> }>

    Uitgang:

     Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS 
  4. Instantiemethode van een willekeurig object van een bepaald type

    Syntaxis:

    (ClassName::methodName) 

    Voorbeeld:

    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);> > }> }>

    Uitgang:

     Geeks For GEEKS 
  5. Klasse Constructeur

    Syntaxis:

    (ClassName::new) 

    Voorbeeld:

    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> );> > }> }>

    Uitgang:

     Hello Geeks Hello For Hello GEEKS