Kaksoispiste (::) -operaattori Javassa

The kaksoispiste (::) -operaattori , tunnetaan myös menetelmäviittausoperaattori Javassa käytetään kutsumaan menetelmää viittaamalla siihen suoraan luokkansa avulla. Ne käyttäytyvät täsmälleen kuten lambda-lausekkeet. Ainoa ero, joka sillä on lambda-lausekkeisiin verrattuna, on se, että tämä käyttää suoraa viittausta menetelmään nimellä sen sijaan, että se antaisi menetelmälle edustajan.

Syntaksi:

 :: 

Esimerkki: Tulosta kaikki streamin elementit seuraavasti:

  • Lambda-lausekkeen käyttäminen:
    stream.forEach( s->System.out.println(s)); 

    Ohjelmoida:




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

    Lähtö:

     Geeks For Geeks A Computer Portal 
  • Kaksoispisteoperaattorin käyttäminen:
    stream.forEach( System.out::println); 

    Ohjelmoida: Havainnollistaa kaksoispisteoperaattorin käyttöä




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

    Lähtö:

     Geeks For Geeks A Computer Portal 
  • Milloin ja miten kaksoispisteoperaattoria käytetään?

    Menetelmäviittausta tai kaksoispisteoperaattoria voidaan käyttää viittaamaan:

    • staattinen menetelmä,
    • instanssimenetelmä tai
    • rakentaja.

    Kuinka käyttää menetelmäviittausta Javassa:

    1. Staattinen menetelmä

      Syntaksi:

      (ClassName::methodName) 

      Esimerkki:

      SomeClass::someStaticMethod 

      Ohjelmoida:




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

      Lähtö:

       Geeks For GEEKS 
    2. Instanssimenetelmä

      Syntaksi:

      (objectOfClass::methodName) 

      Esimerkki:

      System.out::println 

      Ohjelmoida:




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

      Lähtö:

       Geeks For GEEKS 
    3. Super menetelmä

      Syntaksi:

      (super::methodName) 

      Esimerkki:

      super::someSuperClassMethod 

      Ohjelmoida:




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

      Lähtö:

       Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS 
    4. Tietyn tyyppisen mielivaltaisen objektin ilmentymämenetelmä

      Syntaksi:

      (ClassName::methodName) 

      Esimerkki:

      SomeClass::someInstanceMethod 

      Ohjelmoida:




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

      Lähtö:

       Geeks For GEEKS 
    5. Luokan rakentaja

      Syntaksi:

      (ClassName::new) 

      Esimerkki:

      ArrayList::new 

      Ohjelmoida:




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

      Lähtö:

       Hello Geeks Hello For Hello GEEKS