THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

This implicit conversion of primitive types and objects to strings happens only when you're using + or += in
expressions involving strings. It does not happen anywhere else. A method, for example, that takes a String
parameter must be passed a String. You cannot pass it an object or primitive type and have it converted
implicitly.


When a null reference is converted to a String, the result is the string "null", hence a null reference
can be used freely within any string concatenation expression.


9.5. Operator Precedence and Associativity


Operator precedence is the "stickiness" of operators relative to each other. Operators have different
precedences. For example, relational operators have a higher precedence than boolean logic operators, so you
can say


if (min <= i && i <= max)
process(i);


without any confusion. Because * (multiply) has a higher precedence than - (minus), the expression


3 - 3 * 5


has the value 12, not zero. Precedence can be overridden with parentheses; if zero were the desired value, for
example, the following would do the trick:


(3 - 3) * 5


When two operators with the same precedence appear next to each other, the associativity of the operators
determines which is evaluated first. Because + (add) is left-associative, the expression


a + b + c


is equivalent to


(a + b) + c


The following table lists all the operators in order of precedence from highest to lowest. All the operators are
binary, except those shown as unary with expr, the creation and cast operators (which are also unary), and
the conditional operator (which is ternary). Operators with the same precedence appear on the same line of the
table:


postfix operators []. (params)expr++expr--

unary operators ++expr--expr+expr-expr~!
Free download pdf