logo

Operator podwójnego dwukropka (::) w Javie

The operator podwójnego dwukropka (::) , znany również jako operator odniesienia do metody w Javie służy do wywołania metody poprzez bezpośrednie odniesienie się do niej za pomocą jej klasy. Zachowują się dokładnie tak, jak wyrażenia lambda. Jedyna różnica w stosunku do wyrażeń lambda polega na tym, że używa się bezpośredniego odniesienia do metody według nazwy, zamiast zapewniać delegata do metody.

Składnia:



 ::>

Przykład: Aby wydrukować wszystkie elementy strumienia:

  • Używając wyrażenia 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 ->System.out.println(s));> >}> }>

    odczyt pliku csv w Javie

    >

    >

    Wyjście:

     Geeks For Geeks A Computer Portal>
  • Używanie operatora podwójnego dwukropka:
    stream.forEach( System.out::println);>

    Program: Aby zademonstrować użycie operatora podwójnego dwukropka




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

    >

    >

    Wyjście:

     Geeks For Geeks A Computer Portal>

Kiedy i jak używać operatora podwójnego dwukropka?

Odniesienia do metody lub operatora dwukropka można używać do odwoływania się do:

  • metoda statyczna,
  • metoda instancji lub
  • konstruktor.

Jak korzystać z odwołania do metody w Javie:

  1. Metoda statyczna

    Składnia:

    (ClassName::methodName)>

    Przykład:

    SomeClass::someStaticMethod>

    Program:




    czyszczenie pamięci podręcznej npm

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

    >

    >

    Wyjście:

     Geeks For GEEKS>
  2. Metoda instancji

    Składnia:

    (objectOfClass::methodName)>

    Przykład:

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

    >

    >

    alfabet jako cyfry
    Wyjście:

     Geeks For GEEKS>
  3. Super metoda

    Składnia:

    (super::methodName)>

    Przykład:

    super::someSuperClassMethod>

    Program:




    css wyrównywanie obrazów
    // 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);> >}> }>

    >

    >

    Wyjście:

     Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
  4. Metoda instancji dowolnego obiektu określonego typu

    Składnia:

    (ClassName::methodName)>

    Przykład:

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

    >

    >

    Wyjście:

     Geeks For GEEKS>
  5. Konstruktor klasy

    Składnia:

    (ClassName::new)>

    Przykład:

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

    >

    >

    karta SIM jest włożona, ale nie ma usługi Android
    Wyjście:

     Hello Geeks Hello For Hello GEEKS>