go to previous page   go to home page   hear noise   go to next page

Answer:

Yes.

Non-short-circuit Operator

ExpressionResultEvaluation Order
true && true true both operands evaluated
false && true false only first operand evaluated
true && false false both operands evaluated
false && false false only first operand evaluated
true & true true both operands evaluated
false & true false both operands evaluated
true & false false both operands evaluated
false & false false both operands evaluated

The & operator combines two boolean values using the rules for AND, but always evaluates both operands. This is useful when you want both operands to be evaluated no matter what values are returned.

The disadvantage is a possible loss of speed. Most Java programs are written using the && operator whenever an AND is needed. The programmer must be careful of side-effects, though. Usually this is done by writing methods that are "pure functions" as much as possible. A pure function is a method that returns a value and is free of side effects. Methods that are not pure functions should usually be kept out of boolean expressions.

For example, the final version of the method computeMaximum() in the previous example is a pure function. It computed the maximum, but did not itself save the result.

QUESTION 5:

What does the following print?

int count = 0;     // say that these values are the result of
int total = 345;   // the program running with input data.

if ( count > 0 && total / count > 80 )
  System.out.println("Acceptable Average");
else
  System.out.println("Poor Average");