13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

56Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to JavaTable 3.1 Arithmetic and logical Java operators in order of precedenceOperators[] .- ++ -- ! ~(type) new* / %+ -> >>>< > =instanceof== !=&^|&&||?:= += -= *= /=%= =>>>= &= ^= |=Explanati<strong>on</strong>array indexing, member referenceunary operators: negate, increment, decrement, logical-not, bitwise-notcoerci<strong>on</strong>, or casting to a different type; creating a new objectmultiplicati<strong>on</strong>, divisi<strong>on</strong>, remainderadditi<strong>on</strong>, subtracti<strong>on</strong>shift-left, shift-right-sign-extend, shift-right-zero-fillless-than, greater-than, less-or-equal, greater-or-equal, comparing objecttypesequal, not-equalbitwise-and (boolean for boolean operands with no short-circuit) *bitwise-xor (with boolean operands it is a boolean-xor) **bitwise-or (boolean for boolean operands with no short-circuit) *logical-and (with short-circuit) *logical-or (with short-circuit) *Inline if expressi<strong>on</strong>, e.g., a ? b : c says, if a is true, then the valueis b, else it is c.Assignment; those with an operator, as in a op= b will perform theoperati<strong>on</strong> a op b then assign the result back to a.* In Java there are two ways to do a boolean AND operati<strong>on</strong>: using & or &&. Remember thatfor “a AND b”, if either is false, then the result is false. That means that if “a” isfalse, there is no need to evaluate “b” because it will not affect the result. Skipping theevaluati<strong>on</strong> of “b” in this case is called short-circuiting. Java will use short-circuit evaluati<strong>on</strong>when using the && operator, but not &. The same applies to the OR operators || and |where Java can short-circuit <strong>on</strong> a true evaluati<strong>on</strong> of the first operand for ||. This is animportant distincti<strong>on</strong> when “a” and “b” are not just simple variable references but rathermethod calls or other complex expressi<strong>on</strong>s, especially <strong>on</strong>es with side effects.** XOR is exclusive or, where the result of “a XOR b” is true if “a” or “b” is true, but notboth. For bitwise operands, “a” and “b” refer here to bits in the operand; for booleanoperands it is the <strong>on</strong>e value. Examples: 5^6 is 3; true^false is true but true^trueis false.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!