logo

Opérateur double deux-points (::) en Java

Le opérateur double-point (::) , aussi connu sous le nom opérateur de référence de méthode en Java, est utilisé pour appeler une méthode en s'y référant directement à l'aide de sa classe. Ils se comportent exactement comme les expressions lambda. La seule différence avec les expressions lambda est qu'elles utilisent une référence directe à la méthode par son nom au lieu de fournir un délégué à la méthode.

Syntaxe:



 ::>

Exemple: Pour imprimer tous les éléments du flux :

  • Utilisation de l'expression Lambda :
    stream.forEach( s->System.out.println(s));>

    Programme:








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

    quel est le numéro de l'alphabet

    >

    >

    Sortir:

     Geeks For Geeks A Computer Portal>
  • Utilisation de l'opérateur double deux-points :
    stream.forEach( System.out::println);>

    Programme: Pour démontrer l'utilisation de l'opérateur double deux-points




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

    >

    >

    Sortir:

     Geeks For Geeks A Computer Portal>

Quand et comment utiliser l’opérateur double deux-points ?

La référence de méthode ou l’opérateur double deux-points peut être utilisé pour faire référence :

  • une méthode statique,
  • une méthode d'instance, ou
  • un constructeur.

Comment utiliser la référence de méthode en Java :

  1. Méthode statique

    Syntaxe:

    (ClassName::methodName)>

    Exemple:

    SomeClass::someStaticMethod>

    Programme:




    applications cachées sur cet appareil

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

    >

    >

    Sortir:

     Geeks For GEEKS>
  2. Méthode d'instance

    Syntaxe:

    (objectOfClass::methodName)>

    Exemple:

    System.out::println>

    Programme:




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

    >

    >

    différenciation partielle dans le latex
    Sortir:

     Geeks For GEEKS>
  3. Super méthode

    Syntaxe:

    (super::methodName)>

    Exemple:

    super::someSuperClassMethod>

    Programme:




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

    >

    >

    Sortir:

     Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS>
  4. Méthode d'instance d'un objet arbitraire d'un type particulier

    Syntaxe:

    (ClassName::methodName)>

    Exemple:

    SomeClass::someInstanceMethod>

    Programme:




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

    >

    >

    Sortir:

     Geeks For GEEKS>
  5. Constructeur de classe

    Syntaxe:

    (ClassName::new)>

    Exemple:

    ArrayList::new>

    Programme:




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

    >

    >

    tigre comparé au lion
    Sortir:

     Hello Geeks Hello For Hello GEEKS>