Java provides a conditional-and and a conditional-or operator, that both take one or two operands of type boolean and produce a boolean result. These are:
{ boolean L = evaluate(<left-expr>); if (L) { return evaluate(<right-expr>); } else { // short-circuit the evaluation of the 2nd operand expression return false; } }
The evaluation of <left-expr> || >right-expr< is equivalent to the following pseudo-code:
{ boolean L = evaluate(<left-expr>); if (!L) { return evaluate(<right-expr>); } else { // short-circuit the evaluation of the 2nd operand expression return true; } }
As the pseudo-code above illustrates, the behavior of the short-circuit operators are equivalent to using if / else statements.
Example - using && as a guard in an expression
The following example shows the most common usage pattern for the && operator. Compare these two versions of a method to test if a supplied Integer is zero.
public boolean isZero(Integer value) { return value == 0; } public boolean isZero(Integer value) { return value != null && value == 0; }
The first version works in most cases, but if the value argument is null, then a NullPointerException will be thrown.
In the second version we have added a "guard" test. The value != null && value == 0 expression is evaluated by first performing the value != null test. If the null test succeeds (i.e. it evaluates to true) then the value == 0 expression is evaluated. If the null test fails, then the evaluation of value == 0 is skipped (short-circuited), and we don't get a NullPointerException.
Example - using && to avoid a costly calculation
The following example shows how && can be used to avoid a relatively costly calculation:
public boolean verify(int value, boolean needPrime) { return !needPrime | isPrime(value); } public boolean verify(int value, boolean needPrime) { return !needPrime || isPrime(value); }
In the first version, both operands of the | will always be evaluated, so the (expensive) isPrime method will be called unnecessarily. The second version avoids the unnecessary call by using || instead of |.
The operators <, <=, > and >= are binary operators for comparing numeric types. The meaning of the operators is as you would expect. For example, if a and b are declared as any of byte, short, char, int, long, float, double or the corresponding boxed types:
The result type for these operators is boolean in all cases.
Relational operators can be used to compare numbers with different types. For example:
int i = 1; long l = 2; if (i < l) { System.out.println("i is smaller"); }
Relational operators can be used when either or both numbers are instances of boxed numeric types. For example:
Integer i = 1; // 1 is autoboxed to an Integer Integer j = 2; // 2 is autoboxed to an Integer if (i < j) { System.out.println("i is smaller"); }
The precise behavior is summarized as follows:
You need to be careful with relational comparisons that involve floating point numbers:
Finally, Java does bit support the use of relational operators with any types other than the ones listed above. For example, you cannot use these operators to compare strings, arrays of numbers, and so on.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions