Operator două puncte (::) în Java

The operator dublu două puncte (::). , de asemenea cunoscut ca si operator de referință al metodei în Java, este folosit pentru a apela o metodă prin referire la ea cu ajutorul clasei sale direct. Ele se comportă exact ca expresiile lambda. Singura diferență pe care o are față de expresiile lambda este că aceasta folosește referire directă la metodă prin nume în loc să ofere un delegat la metodă.

Sintaxă:

 :: 

Exemplu: Pentru a imprima toate elementele fluxului:

  • Folosind expresia Lambda:
    stream.forEach( s->System.out.println(s)); 

    Program:




    // 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 ->Printlns.out.system(e));>>>

    Ieșire:

     Geeks For Geeks A Computer Portal 
  • Folosind operatorul două puncte:
    stream.forEach( System.out::println); 

    Program: Pentru a demonstra utilizarea operatorului dublu două puncte




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

    Ieșire:

     Geeks For Geeks A Computer Portal 
  • Când și cum să utilizați operatorul două puncte?

    Referința metodei sau operatorul două puncte pot fi utilizate pentru a face referire:

    • o metodă statică,
    • o metodă de instanță sau
    • un constructor.

    Cum să utilizați referința de metodă în Java:

    1. Metoda statica

      Sintaxă:

      (ClassName::methodName) 

      Exemplu:

      SomeClass::someStaticMethod 

      Program:




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

      Ieșire:

       Geeks For GEEKS 
    2. Metoda de instanță

      Sintaxă:

      (objectOfClass::methodName) 

      Exemplu:

      System.out::println 

      Program:




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

      Ieșire:

       Geeks For GEEKS 
    3. Super metoda

      Sintaxă:

      (super::methodName) 

      Exemplu:

      super::someSuperClassMethod 

      Program:




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

      Ieșire:

       Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS 
    4. Metoda de instanță a unui obiect arbitrar de un anumit tip

      Sintaxă:

      (ClassName::methodName) 

      Exemplu:

      SomeClass::someInstanceMethod 

      Program:




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

      Ieșire:

       Geeks For GEEKS 
    5. Constructor de clasă

      Sintaxă:

      (ClassName::new) 

      Exemplu:

      ArrayList::new 

      Program:




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

      Ieșire:

       Hello Geeks Hello For Hello GEEKS