Doubt about Logical Operators && and || in Java

I have the following code in my app:

if (aquaName != null && !aquaName.getText().toString().isEmpty()){
    values.put(NAME_COLUMN, aquaName.getText().toString().trim());
} else {
    Toast.makeText(this, "Name cannot be empty", Toast.LENGTH_SHORT).show();
    return;
}

Why if I use || instead of && Do I not reach condition else?

I've read here on stack about | & || && but I can't understand.

Author: Nakamoto, 2018-06-01

6 answers

The use of the operator && or the operator || will depend on your conditions. The operator && will be used when all conditions must be true, example:

if(a && b && c && d){
    //Se todas forem verdade
}else{
   //Se qualquer delas foram mentira
}

See the letters a, b, c and d as being conditions, so if all of them are true it will enter if, if at least one of them is not true it will enter else.

For the operator ||, called OU, it will allow to enter the if if the minus one condition is true, i.e. if a, b, c or d is true will enter if, if all were false, will enter else, example:

if(a || b || c || d){
    //Se algumas (ao menos uma) for verdade
}else{
   //Se todas forem mentira (falsa)
}

For your example:

if (aquaName != null && !aquaName.getText().toString().isEmpty()){
    values.put(NAME_COLUMN, aquaName.getText().toString().trim());
} else {
    Toast.makeText(this, "Name cannot be empty", Toast.LENGTH_SHORT).show();
    return;
}

Will only enter else if aquaName is null and (&&) if it is empty.

 7
Author: Edeson Bizerril, 2018-06-02 13:43:09

Because || (or) if any of the conditions returns true it executes if.

Example:

aquaName = "" // (vazio)
aquaName != null // verdadeiro
!aquaName.getText().toString().isEmpty() // falso

Running Your if, already using && only if all conditions are true that it runs Your if, if it doesn't fall into else.

 4
Author: Laerte, 2018-06-01 12:21:49

By parts:

  • aquaName != null will be true if aquaName is not null, i.e. has any value ("test","","", etc)
  • !aquaName.getText().toString().isEmpty() will be true if it is empty (on account of !).

Using ||, I would enter if if one of the conditions were true. Considering what you have, if your first condition is true, the second is false, and vice versa.


For operators, use a (| &) or two (|| &&) "only" forces the execution of what comes next; using as an example its condition,

if (aquaName != null && !aquaName.getText().toString().isEmpty())

In this case, if aquaName is different from null, it already enters if. Using only one operator,

if (aquaName != null & !aquaName.getText().toString().isEmpty())

Even if aquaName equals null, it will do validation !aquaName.getText().toString().isEmpty().

I suggest you give a read in this article , it will help you understand well when to use this type of comparator.

 1
Author: rLinhares, 2018-06-01 12:11:46

|| equals " or "and && equals"and"

In its condition the first one has to be different from null and the second one has to be different from empty.

Then the two conditions have to be true, if it were used || only one would already appear as true.

 1
Author: Edson Reis, 2018-06-01 12:15:11

Https://pt.wikipedia.org/wiki/Teoremas_de_De_Morgan

If you observe, the Block else houses a message saying that Name cannot be empty.

Thus, the Block if should house a case that

  • let be the negation of the case else
  • The Block else should execute if:
    • aquaName for null or
    • the contents of aquaName without the surrounding spaces in it result in an empty string

By theorem of DeMorgan, occurs the passage from the first to the second line, below:

NÃO (aquaName for nulo OU o conteúdo de aquaName sem os espaços circundantes nele não resultar numa string vazia) =

= NÃO (aquaName for nulo) E NÃO (o conteúdo de aquaName sem os espaços circundantes nele não resultar numa string vazia) =

= aquaName != null && !aquaName.getText().toString().isEmpty()
 1
Author: Marcelo Shiniti Uchimura, 2018-06-01 12:24:56

Complementing the answers already given, but naming the oxen, || and && are what is called short-circuit evaluation. This means that if the first part of the expression being evaluated (i.e. the one on the left) is true for the tested expression, the second part of the nem is checked.

String nome = "Luis";
int idade = 10;

if(nome == null && idade == 10) {
//não entra aqui
}

For the code of a block if to be executed, the evaluated expression must be true (true). When using the && operator, this means that both sides they must be true for the expression to be true. In the example, when evaluating the first part of the expression, nome == null, it is false, so the compiler already knows that there is no longer how the expression as a whole is true (both sides must be true, Remember?), therefore, he does not even evaluate the right side of the expression. That's why it is called short circuit, the compiler does not even go to the end of the evaluation, exits before.

if(nome == null || idade == 10) {
//o código aqui dentro é executado
}

An evaluation with ||, in turn, requires that only one side of the expression is true so that the expression as a whole is also true. Thus, nome == null is false, but since there is still another side to evaluate and that can be true, the compiler evaluates it as well. Since idade == 10 is true, the expression as a whole returns true and the code inside the if is executed.

If the first side was already true in the face, the compiler would not even need to evaluate the other side, since, as said, with || it is enough for one side to be true so that the expression as a whole also let true. It would again be a short circuit.

 1
Author: StatelessDev, 2018-06-01 14:12:50