except that the expression on the left-hand side of the assignment is evaluated only once. In the example,
arr[where()] is evaluated only once in the first expression, but twice in the second expressionas you
learned earlier with the ++ operator.
Given the variable var of type T, the value expr, and the binary operator op, the expression
var op= expr
is equivalent to
var = (T) ((var) op (expr))
except that var is evaluated only once. This means that op= is valid only if op is valid for the types
involved. You cannot, for example, use <<= on a double variable because you cannot use << on a double
value.
Note the parentheses used in the expanded form you just saw. The expression
a *= b + 1
is analogous to
a = a * (b + 1)
and not to
a = a * b + 1
Although a+= 1 is the same as ++a and a++, using ++ is considered idiomatic and is preferred.
Exercise 9.3: Review your solution to Exercise 7.3 to see if it can be written more clearly or succinctly with
the operators you've learned about in this chapter.
9.2.8. String Concatenation Operator
You can use + to concatenate two strings. Here is an example:
String boo = "boo";
String cry = boo + "hoo";
cry += "!";
System.out.println(cry);