Pergunta

I have a question in regards to Java conditional if statements. If I have a statement such as this:

if(true || false){
//output
}

Will Java go inside of the the if statement as soon as it sees the true statement or will it still evaluate the false statement? I ask because I will have a condition statement which will have a few conditional tests, each which will require a call to a database:

if(isDatabase1() || isDatabase2() || isDatabase3()){
}

The first test will be a condition which will most likely return true, so I was wondering if it will stop after this and go straight into the if statement, or if it will still test the remaining statements (which then will require useless database calls)?

Foi útil?

Solução

No, they short circuit. More specifically if isDatabase1 returns true then neither isDatabase2 or isDatabase3 will be called. https://en.wikipedia.org/wiki/Short-circuit_evaluation

Licenciado em: CC-BY-SA com atribuição
scroll top