![]() The || operator is like | (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is false. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.Īnd similarly, Section 15:24, Conditional-Or operator (||), says: Thus, & computes the same result as & on boolean operands. If the value of the left-hand operand is true, then the right-hand expression is evaluated the resulting value becomes the value of the conditional-and expression. At run time, the left-hand operand expression is evaluated first if the resulting value is false, the value of the conditional-and expression is false and the right-hand operand expression is not evaluated. The & operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true. Section 15:23, Conditional-And operator (&), says: XOR can't be optimized, because it depends on both parameters.Īll the answers here are great but, just to illustrate where this comes from, for questions like this it's good to go to the source: the Java Language Specification. The first parameter is checked and it returns true, so the second paramter won't be checked, because the result is true anyway. if (aString = null || !aString.equals("lala")) The same for OR: if (aString = null | !aString.equals("lala")) The first parameter is checked and it returns false, so the second paramter won't be checked, because the result is false anyway. if (aString != null & aString.equals("lala")) Given for all examples: String aString = null ĪND: if (aString != null & aString.equals("lala"))īoth parameters are checked before the evaluation is done and a NullPointerException will be thrown for the second parameter. ![]() ![]() Sound compilcated? An easy example should make it clear: The double ones will first check the left parameter and its value and if true ( ||) or false ( &) leave the second one untouched. The single ones will check every parameter, regardless of the values, before checking the values of the parameters. & and & are "and" operators, | and || "or" operators, ^ is "xor" ![]() Java has 5 different boolean compare operators: &, &, |, ||, ^ ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |