What is the difference and what are the & & & & e | and || in Java?

20

I was analyzing some possibilities for the implementation of an algorithm and I went to search for this type of & and | , and I read some topics in English but it was not 100% clear to me what it is and where I can use it, it. Then I would like an explanation, clearer and with practical examples of how to use it. (And in my native language, in the case: Portuguese ).

Questions

  • What is the difference between the & and && operators?
  • What is the & ?
  • An example usage for the & operator?
  • What is the difference between the | and || operators?
  • What is the | ?
  • An example usage for the | operator?
asked by anonymous 04.05.2015 / 20:48

2 answers

18

The difference between logical operators that use a symbol and two symbols is that when using two is what is known as short circuit logic .

The short-circuit logic operator performs as little code as possible in order to process the logical operation, ie if you are doing a if(false && true) comparison when processing this section and find that the first result is false nor does it even analyze the second part of the logic, so what if the second part is true or false , because false && qualquerCoisa always gives false .

In general, short circuit logic comparators are always used because they will never be slower than non-short circuit logic operators. However, it is necessary to be aware of exceptional situations where the execution of the rest of the operators can change the flow of the program and generate an unexpected result, for example:

int a = 5;
if(++a > 5 || ++a > 6) a++; 
System.out.println(a);       //a==7

Against:

int b = 5;
if(++b > 5 | ++b > 6) b++; 
System.out.println(b);       //b==8

Example on Ideone

In the first case, because it is a short circuit, it makes the first ++a , verifies that it is greater than 5, and does not continue executing the code after || , since true OU qualquerCoisa is true . Since it gave if(true) it enters if and makes a++ , making it 7 .

In the second case, because it is not using short-circuit operators, it processes everything inside if , so it ++b twice, resulting in if(true|true) which is the same as if(true) it enters the code inside the if and makes b++ making it 8 .

    
04.05.2015 / 21:12
19

& is a binary operation, see more at Wikipedia - Binary Logic , Example:

int a = 60; // Em binário, 60 é 0011 1100
int b = 13; // Em binário, 13 é 0000 1101
int c = a & b; // Complicado, mas quando os dois dígitos forem 1, o resultado será 1, se não será 0. Ou seja, "c" dará 0000 1100, que é 12.

& &; means "and", or "e", for example:

if (1 == 1 && 6 > 9) {
    // Se 1 for igual a 1 E 6 for maior que 9, isso vai acontecer, no caso não, pois 6 não é maior que 9.
}

| is another binary operation, where if any of the binary digits are 1, the result will be an example:

int a = 60; // Em binário, 60 é 0011 1100
int b = 13; // Em binário, 13 é 0000 1101
int c = a | b; // Quando um dos dígitos forem 1, o resultado será 1 também. "c" dará 0011 1101, que é 61.

|| means "or", or "or", for example:

if (1 == 1 || 6 > 9) {
    // Se 1 for igual a 1 OU 6 for maior que 9, isso vai acontecer, no caso sim, pois 1 é igual a 1, e isso já basta para o |.
}
    
04.05.2015 / 21:01