Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
is the type of System.out.println functional interface?

I do not understand one thing, should not the type of the method reference be a functional interface? So how do we pass System.out::println as a parameter to forEach?in the following code :

1-is System.out::println a Consumer?

2-if yes what is its generic type? I do not mean the type of list that is calling the method, but the generic type of the method reference that passed to the forEach.

Consumer<Integer> consumer=System.out::println;
numbers.forEach(consumer); 
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The type of System.out.println is not a functional interface, since System.out.println is a method, not an interface.

However, the method reference System.out::println can be used as an implementation of a functional interface. Since println accepts an argument and returns nothing, you can say that it "consumes" the argument you pass to it. Therefore it fits as an implementation of the Consumer<T> functional interface.

Note the there are multiple variants of println, which can be used to implement different functional interfaces.

For example:

  • A method reference to public void println(int x) can implement IntConsumer.

  • A method reference to public void println(double x) can implement DoubleConsumer.

  • A method reference to public void println(Object x) can implement Consumer<T>.

over 4 years ago · Santiago Trujillo Relatório

0

  1. System.out.println is method not an interface
  2. System.out::println can be treated as an impl of interface since it takes an argument and returns nothing
over 4 years ago · Santiago Trujillo Relatório

0

This part of code:

Consumer<Integer> consumer=System.out::println;

is equal to this lambda expression:

Consumer<Integer> consumer=i->System.out.println(i);

and above code block could be written in this way as an anonymous inner class using Consumer interface:

Consumer<Integer> consumer=new Consumer<Integer>(){
   public void accept(Integer i){
      System.out.println(i);
   }
}

So "System.out.println" itself it is not a functional interface, but when you use :: you implicitly implement the Consumer interface.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda