Doubt about logical operators && e || in Java

3

I have the following code in my application:

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 should I use || instead of && I do not reach the else condition?

I already read here on the stack about | & || && but I can not understand.

    
asked by anonymous 01.06.2018 / 14:02

6 answers

6

Using the && operator or the || operator will depend on your conditions. The && operator will be used when all conditions are MUST be true, eg:

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

See the letters a , b , c and d as 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 you to enter if if at least one condition is true, ie a , b , c or d for 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.

    
01.06.2018 / 14:25
4

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

Example:

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

Executing your if, already using && only if all conditions are true that it executes its if, if it does not fall on the else.

    
01.06.2018 / 14:09
1

In parts:

  • % will be true if aquaName != null is not null, that is, it has any value ("test", "", "", etc.)
  • aquaName will be true if it is empty (because of !aquaName.getText().toString().isEmpty() ).

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

As for the operators, using a ( if | ) or two ( & || ) "only" forces the execution of what comes next; using your condition as an example

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

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

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

Even though if is equal to aquaName , it will validate null .

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

    
01.06.2018 / 14:11
1

link

If you notice, the block else houses a message saying Name cannot be empty .

So, the if block should house a case that

  • be the denial of the case else
  • block else should run if:
    • aquaName for null OU
    • The content of aquaName without the surrounding spaces in it results in an empty string

By DeMorgan's theorem, we pass 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()
    
01.06.2018 / 14:17
1

Complementing the answers already given, but giving names to the steers, || and && are what is called short-circuit evaluation. This means that if the first part of the expression being evaluated (ie, the one on the left) is true for the expression tested, the second part of the nor is checked.

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

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

For the code of a if block to be executed, the evaluated expression must be true ( true ). When using the && operator, this means that both sides must be true so that the expression is true . In the example, when evaluating the first part of the expression, nome == null , it is false, so the compiler already knows that the expression as a whole is no longer true (both sides must be true , remember? ), so he does not even evaluate the right side of the expression. This is why it's called a short circuit, the compiler does not even go to the end of the evaluation, it leaves 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 be true so that the expression as a whole is also true . Thus, nome == null is false, but since there is still another side to be evaluated that can be true , the compiler also evaluates it. As idade == 10 is true, the expression as a whole returns true and the code inside if is executed.

If the first side is already true face, the compiler does not even have to evaluate the other side, since, as said, with || , only one side is true so that the expression as a whole is also true . It would again be a short circuit.

    
01.06.2018 / 16:12
0

|| is equivalent to "or" and && is equivalent to "e"

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

then the two conditions have to be true, if you used || only one would already be set to true.

    
01.06.2018 / 14:15