logo

Dvojtečka (::) operátor v Javě

The operátor dvojtečky (::). , také známý jako operátor reference metody v Javě se používá k volání metody přímým odkazem na ni pomocí své třídy. Chovají se přesně jako lambda výrazy. Jediný rozdíl, který má od výrazů lambda, je ten, že se používá přímý odkaz na metodu podle názvu namísto poskytování delegáta metodě.

Syntax:



 ::>

Příklad: Chcete-li vytisknout všechny prvky streamu:

  • Použití výrazu Lambda:
    stream.forEach( s->System.out.println(s));>

    Program:

    kat timpf sestra








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

    >

    >

    Výstup:

     Geeks For Geeks A Computer Portal>
  • Použití operátoru dvojité dvojtečky:
    stream.forEach( System.out::println);>

    Program: Demonstrovat použití operátoru dvojité dvojtečky




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

    >

    >

    Výstup:

     Geeks For Geeks A Computer Portal>

Kdy a jak používat operátor dvojtečky?

Odkaz na metodu nebo operátor dvojité dvojtečky lze použít k odkazování:

  • statická metoda,
  • instanční metoda, popř
  • konstruktér.

Jak používat odkaz na metodu v Javě:

  1. Statická metoda

    Syntax:

    (ClassName::methodName)>

    Příklad:

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

    >

    >

    Výstup:

     Geeks For GEEKS>
  2. Instanční metoda

    Syntax:

    (objectOfClass::methodName)>

    Příklad:

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

    >

    >

    Výstup:

     Geeks For GEEKS>
  3. Super metoda

    Syntax:

    (super::methodName)>

    Příklad:

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

    >

    souborový systém v linuxu

    >

    Výstup:

     Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
  4. Instanční metoda libovolného objektu určitého typu

    Syntax:

    (ClassName::methodName)>

    Příklad:

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

    >

    >

    Výstup:

     Geeks For GEEKS>
  5. Konstruktor třídy

    Syntax:

    (ClassName::new)>

    Příklad:

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

    >

    >

    Výstup:

     Hello Geeks Hello For Hello GEEKS>