logo

Dvigubas dvitaškis (::) operatorius Java

The dvitaškis (::) operatorius , taip pat žinomas kaip metodo nuorodos operatorius Java, naudojamas metodui iškviesti, tiesiogiai nurodant jį savo klasės pagalba. Jie elgiasi tiksliai taip, kaip lambda išraiškos. Vienintelis jos skirtumas nuo lambda išraiškų yra tas, kad ji naudoja tiesioginę nuorodą į metodą pagal pavadinimą, o ne suteikia metodo įgaliotinį.

Sintaksė:



 ::>

Pavyzdys: Norėdami spausdinti visus srauto elementus:

  • Naudojant Lambda išraišką:
    stream.forEach( s->System.out.println(s));>

    Programa:








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

    >

    >

    Išvestis:

     Geeks For Geeks A Computer Portal>
  • Naudojant dvigubo dvitaškio operatorių:
    stream.forEach( System.out::println);>

    Programa: Pademonstruoti dvigubo dvitaškio operatoriaus naudojimą

    java skaitytuvas kitas




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

    >

    >

    Išvestis:

     Geeks For Geeks A Computer Portal>

Kada ir kaip naudoti dvigubo dvitaškio operatorių?

Metodo nuoroda arba dvigubo dvitaškio operatorius gali būti naudojamas nurodyti:

  • statinis metodas,
  • atvejo metodas arba
  • konstruktorius.

Kaip naudoti metodo nuorodą Java:

  1. Statinis metodas

    Sintaksė:

    (ClassName::methodName)>

    Pavyzdys:

    SomeClass::someStaticMethod>

    Programa:




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

    >

    >

    Išvestis:

     Geeks For GEEKS>
  2. Instancijos metodas

    Sintaksė:

    (objectOfClass::methodName)>

    Pavyzdys:

    System.out::println>

    Programa:




    abėcėlė sunumeruota

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

    >

    >

    Išvestis:

     Geeks For GEEKS>
  3. Super metodas

    Sintaksė:

    (super::methodName)>

    Pavyzdys:

    super::someSuperClassMethod>

    Programa:




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

    >

    >

    Išvestis:

     Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
  4. Savavališko tam tikro tipo objekto egzempliorių metodas

    Sintaksė:

    (ClassName::methodName)>

    Pavyzdys:

    SomeClass::someInstanceMethod>

    Programa:




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

    abėcėlė kaip skaičiai

    >

    >

    Išvestis:

     Geeks For GEEKS>
  5. Klasės konstruktorius

    Sintaksė:

    (ClassName::new)>

    Pavyzdys:

    ArrayList::new>

    Programa:




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

    >

    >

    Išvestis:

     Hello Geeks Hello For Hello GEEKS>