Dobbel kolon (::) operatør i Java

De dobbel kolon (::) operator , også kjent som metodereferanseoperatør i Java, brukes til å kalle en metode ved å referere til den ved hjelp av klassen direkte. De oppfører seg akkurat som lambda-uttrykkene. Den eneste forskjellen den har fra lambda-uttrykk er at denne bruker direkte referanse til metoden ved navn i stedet for å gi en delegat til metoden.

Syntaks:

 :: 

Eksempel: Slik skriver du ut alle elementene i strømmen:

  • Bruke Lambda-uttrykk:
    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 ->System.out.println(s));> > }> }>

    Produksjon:

     Geeks For Geeks A Computer Portal 
  • Bruk av dobbel kolon-operator:
    stream.forEach( System.out::println); 

    Program: For å demonstrere bruken av dobbel kolon-operator




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

    Produksjon:

     Geeks For Geeks A Computer Portal 

Når og hvordan bruker du dobbel kolon-operator?

Metodereferanse eller dobbel kolon-operator kan brukes til å referere:

  • en statisk metode,
  • en instansmetode, eller
  • en konstruktør.

Slik bruker du metodereferanse i Java:

  1. Statisk metode

    Syntaks:

    (ClassName::methodName) 

    Eksempel:

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

    Produksjon:

     Geeks For GEEKS 
  2. Forekomstmetode

    Syntaks:

    (objectOfClass::methodName) 

    Eksempel:

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

    Produksjon:

     Geeks For GEEKS 
  3. Super metode

    Syntaks:

    (super::methodName) 

    Eksempel:

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

    Produksjon:

     Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS 
  4. Forekomstmetode for et vilkårlig objekt av en bestemt type

    Syntaks:

    (ClassName::methodName) 

    Eksempel:

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

    Produksjon:

     Geeks For GEEKS 
  5. Klasse konstruktør

    Syntaks:

    (ClassName::new) 

    Eksempel:

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

    Produksjon:

     Hello Geeks Hello For Hello GEEKS