Java Reference
In-Depth Information
is the same as
Both statements assign to x the value of x minus 100.
This shorthand will work for all the binary operators in Java (that is, those that require
two operands). The general form of the shorthand is
var op = expression;
Thus, the arithmetic and logical shorthand assignment operators are the following:
Because these operators combine an operation with an assignment, they are formally re-
ferred to as compound assignment operators.
The compound assignment operators provide two benefits. First, they are more compact
than their “longhand” equivalents. Second, in some cases, they are more efficient. For these
reasons, you will often see the compound assignment operators used in professionally writ-
ten Java programs.
Type Conversion in Assignments
In programming, it is common to assign one type of variable to another. For example, you
might want to assign an int value to a float variable, as shown here:
When compatible types are mixed in an assignment, the value of the right side is automat-
ically converted to the type of the left side. Thus, in the preceding fragment, the value in i is
converted into a float and then assigned to f . However, because of Java's strict type check-
Search WWH ::




Custom Search