Java Logical Operator Shorted

Which set is short-circuited, and what exactly means that a complex conditional expression is short-circuited?

public static void main(String[] args) { int x, y, z; x = 10; y = 20; z = 30; // TT // TF // FT // FF //SET A boolean a = (x < z) && (x == x); boolean b = (x < z) && (x == z); boolean c = (x == z) && (x < z); boolean d = (x == z) && (x > z); //SET B boolean aa = (x < z) & (x == x); boolean bb = (x < z) & (x == z); boolean cc = (x == z) & (x < z); boolean dd = (x == z) & (x > z); } 
+64
java logical-operators
Jan 06 2018-12-12T00:
source share
8 answers

Operators && and || "short circuit", which means that they do not evaluate the right side if it is not necessary.

Operators & and | used as logical operators always evaluate both sides.

There is only one short circuit case for each operator, and they are:

  • false && ... - no need to know what the right side is, the result should be false
  • true || ... true || ... - no need to know what the right side is, the result must be true

Compare the behavior in a simple example:

 public boolean longerThan(String input, int length) { return input != null && input.length() > length; } public boolean longerThan(String input, int length) { return input != null & input.length() > length; } 

In version 2, the operator is used without short circuit & and will NullPointerException if input is null , but the first version will return false without exception;

+177
Jan 06 2018-12-12T00:
source share

SET A uses short-circuited Boolean operators.

What β€œshort circuit” means in the context of logical operators is that for a set of logical elements b1, b2, ..., bn, the short circuit versions stop evaluating as soon as the first of these logical values ​​is true (||) or false ( &).

For example:

 // 2 == 2 will never get evaluated because it is already clear from evaluating // 1 != 1 that the result will be false. (1 != 1) && (2 == 2) // 2 != 2 will never get evaluated because it is already clear from evaluating // 1 == 1 that the result will be true. (1 == 1) || (2 != 2) 
+7
Jan 06 2018-12-12T00:
source share

A short circuit means that the second operator will not be verified if the first operator accepts the final result.

eg. Expression: True || False

In the case of || all we need is one side like True. Therefore, if the left side is true, there is no point in checking the right side, and therefore it will not be checked at all.

Similarly, False && True

In the case of & &, we need both sides to be true. Therefore, if the left side is False, it makes no sense to check the right side, the answer should be False. And therefore, this will not be verified at all.

+4
Jan 6 2018-12-12T00:
source share
 boolean a = (x < z) && (x == x); 

This type will be short-circuited, which means that if (x < z) evaluates to false, then the latter is not evaluated, a will be false, otherwise && will also evaluate (x == x) .

& is a bitwise operator, but also a Boolean operator AND, which does not close.

You can check them as follows (see how many times the method is called in each case):

 public static boolean getFalse() { System.out.println("Method"); return false; } public static void main(String[] args) { if(getFalse() && getFalse()) { } System.out.println("============================="); if(getFalse() & getFalse()) { } } 
+3
Jan 06 2018-12-12T00:
source share

In simple terms, shorting means stopping the evaluation as soon as you know that the answer can no longer change. For example, if you evaluate a logical AND chain and find FALSE in the middle of this chain, you know that the result will be false, no matter what the values ​​of the other expressions in the chain are. The same goes for the OR s chain: as soon as you find TRUE , you immediately know the answer and therefore you can skip evaluating the rest of the expressions.

You point to Java that you want a short circuit using && instead of & and || instead of | . The first set in your message is a short circuit.

Note that this is more than trying to save multiple processor cycles: in expressions like

 if (mystring != null && mystring.indexOf('+') > 0) { ... } 

a short circuit means the difference between proper operation and failure (in the case where mystring is null).

+2
Jan 6 2018-12-12T00:
source share

Java provides two interesting Boolean operators not found in most other computer languages. These secondary versions of AND and OR are known as logical short circuit operators. As the previous table shows, the OR operator returns true when A is true, regardless of what B.

Similarly, the AND operator results in false when A is false, regardless of what B. is. If you use the forms || and && , not forms | and & these operators, Java will not try to evaluate only the right operand. This is very useful when the right operand depends on whether the left operand is true or false for proper operation.

For example, the following code snippet shows how you can use a logical evaluation of a short circuit to make sure that the division operation is valid before it is evaluated:

 if ( denom != 0 && num / denom >10) 

Since the short-circuited AND ( && ) form is used, there is no risk of throwing an exception at runtime to zero. If this line of code was written using a single AND version, both sides would have to be evaluated, which would throw a runtime exception when denom is zero.

Standard practice is to use the AND and OR short-circuit forms in cases involving Boolean logic, leaving single-character versions exclusively for bitwise operations. However, there are exceptions to this rule. For example, consider the following statement:

  if ( c==1 & e++ < 100 ) d = 100; 

Here, using a single & , the increment to e operation will be performed if c is 1 or not.

+2
Jul 24 '13 at 19:07
source share

Logical OR: - returns true if at least one of the operands evaluates to true. Both operands are evaluated before using the OR operator.

Short circuit OR: - if the operand of the left side returns true, it returns true without evaluating the operand of the right side.

+2
Apr 09 '16 at 16:25
source share
 if(demon!=0&& num/demon>10) 

Since the AND (& &) short circuit form is used, there is no risk of throwing an exception at runtime when the daemon is zero.

Ref. Java 2 Fifth Edition by Herbert Schildt

+1
Jul 12 '13 at 5:07 on
source share



All Articles