Java의 이중 콜론(::) 연산자

그만큼 이중 콜론(::) 연산자 , 또한 ~으로 알려진 메소드 참조 연산자 Java에서는 클래스를 직접 참조하여 메소드를 호출하는 데 사용됩니다. 이는 람다 표현식과 똑같이 동작합니다. 람다 식과의 유일한 차이점은 메서드에 대리자를 제공하는 대신 이름으로 메서드에 대한 직접 참조를 사용한다는 것입니다.

통사론:

 :: 

예: 스트림의 모든 요소를 ​​인쇄하려면 다음을 수행하십시오.

  • 람다 표현식 사용:
    stream.forEach( s->System.out.println(s)); 

    프로그램:




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

    산출:

     Geeks For Geeks A Computer Portal 
  • 이중 콜론 연산자 사용:
    stream.forEach( System.out::println); 

    프로그램: 이중 콜론 연산자의 사용을 보여주기 위해




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

    산출:

     Geeks For Geeks A Computer Portal 

이중 콜론 연산자는 언제, 어떻게 사용하나요?

메소드 참조 또는 이중 콜론 연산자를 사용하여 다음을 참조할 수 있습니다.

  • 정적 방법,
  • 인스턴스 메소드 또는
  • 생성자.

Java에서 메소드 참조를 사용하는 방법:

  1. 정적 방법

    통사론:

    (ClassName::methodName) 

    예:

    SomeClass::someStaticMethod 

    프로그램:




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

    산출:

     Geeks For GEEKS 
  2. 인스턴스 방법

    통사론:

    (objectOfClass::methodName) 

    예:

    System.out::println 

    프로그램:




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

    산출:

     Geeks For GEEKS 
  3. 슈퍼 메소드

    통사론:

    (super::methodName) 

    예:

    super::someSuperClassMethod 

    프로그램:




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

    산출:

     Hello Geeks Bye Geeks Hello For Bye For Hello GEEKS Bye GEEKS 
  4. 특정 유형의 임의 객체의 인스턴스 메서드

    통사론:

    (ClassName::methodName) 

    예:

    SomeClass::someInstanceMethod 

    프로그램:




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

    산출:

     Geeks For GEEKS 
  5. 클래스 생성자

    통사론:

    (ClassName::new) 

    예:

    ArrayList::new 

    프로그램:




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

    산출:

     Hello Geeks Hello For Hello GEEKS