All the OperatorsPrecedencePostfix operatorsUnary prefix operatorsObject creation and castingMultiplicative operatorsAdditive operatorsShift operatorsRelational operatorsA beginner’s errorEquality and inequalityANDExclusive ORORThe ternary operatorThe assignment operators IThe assignment operators IIThe assignment operators IIIThe assignment operators IVWhat you need to knowThe EndJan 14, 2019All the Operators2PrecedenceAn operator with higher precedence is done earlier (prededes) one with lower precedenceA higher precedence is indicated with a lower number; zero is the highest precedenceMost of the time, operators with equal precedence are done left to rightExamples: 3 + 4 * 5 gives 23 10 - 5 - 2 gives 3Exceptions: unary operators, casts, assignment operators, the ternary operator: all done right to left3Postfix operatorsPostfix operators have the highest precedence(parameters) Parameter lists[ ] Brackets indicate indexing into an array. Accesses methods and variablesexpr++, expr-- Postincrement, postdecrement4Unary prefix operatorsUnary prefix operators have the next highest precedence:++expr Preincrement--expr Predecrement+ - Unary plus and unary minus ! Logical negation (not)~ Bitwise complement (invert every bit)5Object creation and casting new Create a new instance of a class (type) Cast (convert) to the given typeSlides are in order of decreasing precedenceHigher precedence means “more tightly bound”The lowest precedence operator is the “main” operator in an expressionFrequently the lowest precedence operator is assignment, for example x = y + z;6Multiplicative operators* Multiply/ Divide% ModulusThese all have the same precedence7Additive operators+ Add- Subtract8Shift operators<< Left shift, end offFor small integers, this is equivalent to multiplying by a power of twoExample: 100 << 3 gives 800>> Right shift with sign extensionFor small integers, this is equivalent to an integer divide by a power of twoExample: 100 >> 2 gives 25>>> Right shift with zero fillDoes not make sense for numbers9Relational operators< Less than<= Less than or equal to> Greater than>= Greater than or equal toinstanceof Left operand is an object whose type (class or interface) is the right operandThese all have the same precedence, and it is higher than equality/inequality tests10A beginner’s errorif (0 <= i < a.length) { ... }Operations are done left to right0 <= i will be either true or falseNeither true < a.length nor false < a.length is legalThe correct expression should beif (0 <= i && i < a.length) { ... }11Equality and inequality== Test if equalFor primitive types, tests if the values are equalFor objects, tests if both sides refer to the same object!= Test if not equalFor primitive types, tests if the values are unequalFor objects, tests if the sides refer to different objectsReminder: these tests should not be used on floating-point numbers (float or double)12AND& ANDFor integral types, ANDs each corresponding pair of bits0 & 0 == 00 & 1 == 01 & 0 == 01 & 1 == 1For booleans, performs the logical AND operationBoolean & is like &&, but both operands are evaluated, even if it is possible to decide the result from the left operand alone13Exclusive OR^ XORFor integral types, XORs each corresponding pair of bits0 ^ 0 == 00 ^ 1 == 11 ^ 0 == 11 ^ 1 == 0For booleans, performs the logical XOR operationa ^ b is true if either a is true or b is true, but not bothThere is no ^^ operation14OR| ORFor integral types, ORs each corresponding pair of bits0 | 0 == 00 | 1 == 1 1 | 0 == 1 1 | 1 == 1For booleans, performs the logical OR operationBoolean | is like ||, but both operands are evaluated, even if it is possible to decide the result from the left operand alone15The ternary operatorboolean-expr ? expression-1 : expression-2This is like if-then-else for values rather than for statementsIf the boolean-expr evaluates to true, the result is expression-1, else it is expression-2Example: max = a > b ? a : b ; sets the variable max to the larger of a and b expression-1 and expression-2 need not be the same type, but either result must be useableThe ternary operator is right associative!To avoid confusion, use parentheses if your expression has more than one ternary operator16The assignment operators IThe assignment operators have the lowest precedenceAssignment is an operationAssignment is right associativea = b = c = 7.5 * w;assigns 7.5*w to c, then assigns c to b, then assigns b to a – if all these assignments are legalExample: if ((line = reader.newLine()) == null) { ... }17The assignment operators IIThere are a lot of assignment operations besides =variable += expression means the same as variable = variable + expressionvariable -= expression means the same as variable = variable - expressionvariable *= expression means the same as variable = variable * expressionvariable /= expression means the same as variable = variable / expression18The assignment operators IIIvariable %= expression means the same as variable = variable % expressionvariable <<= expression means the same as variable = variable << expressionvariable >>= expression means the same as variable = variable >> expressionvariable >>>= expression means the same as variable = variable >>> expression19The assignment operators IVvariable &= expression means the same as variable = variable & expressionvariable |= expression means the same as variable = variable | expressionvariable ^= expression means the same as variable = variable ^ expression20What you need to knowYou should understand what each operator doesParameter lists, array indexing, casting, postfix ++ and --, and the dot operator are done firstIn particular, a cast refers to the one following entity, so to cast the result of an expression you need extra parenthesesExample 1: variable = (type)(expression);Example 2: variable = ((type)variable).method();In arithmetic, the unary operators + and – are done first, then multiplication and division, then addition and subtractionAll assignment operators are done lastFor anything else, it’s a good idea to use parentheses anyway (even if you remember the order of precedence, other people won’t)21The
View Full Document