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

223
Visualizações
How exit from method in a switch expression?

I'm studying switch expression, and I'd like to know how to pass control to the invoker method because "yield" just exits from the switch expression. I can't find any way to make it behave like the classic switch statement.

Is it even possible?

UPDATE: Here's a little piece of code.

public static boolean isTimeToParty(DayOfWeek day) {
    boolean isParty = switch (day) {
        case MONDAY -> false;
        case TUESDAY -> {
            yield false; //return not allowed
        }
        case WEDNESDAY -> false;
        case THURSDAY -> false;
        case FRIDAY -> false;
        case SATURDAY -> true;
        case SUNDAY -> true;
    };
    return isParty;
}

What I mean is: How to avoid assigning value to a variable and then calling return [variable_name]? I would like to return value and exit switch at the same time.

Thanks in advance

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Simply return to the calling method. If you just want to stop processing further cases in the same switch, use break. This and the below example apply to the switch statement, whereas the original question was releated to the switch expression.

public static void main(String[] args) throws Exception {

    switch(1) {
        case 1:
            System.out.println("Case one");
            // no break, so case processing goes one
        case 2:
            System.out.println("Case two");
            break; // we bail out here and do not process further cases
        default:
            System.out.println("Default case");
    }
    
    switch(2) {
        case 2:
            return;
        default:
            System.out.println("Default case");
    }
    
    System.out.println("This is the end - should not be printed");
}

generates output

Case one
Case two
over 4 years ago · Santiago Trujillo Relatório

0

As you cannot use return or break within the switch, it would be easier in your case to simplify with return switch ... directly:

public static boolean isTimeToParty(DayOfWeek day) {
    return switch (day) {
        case SATURDAY, SUNDAY -> true;
        default  -> false;
    };
}
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