Java Reference
In-Depth Information
TABLE 2.7
Continued
Operator
Meaning
Decrement
Add and assign
+=
Subtract and assign
-=
Multiply and assign
*=
Divide and assign
/=
Modulus and assign
%=
2
Q&A
Q What happens if I assign an integer value to a variable that is too large for
that variable to hold?
A Logically, you might think that the variable is converted to the next larger type, but
this isn't what happens. Instead, an overflow occurs—a situation in which the num-
ber wraps around from one size extreme to the other. An example of overflow
would be a byte variable that goes from 127 (acceptable value) to 128 (unaccept-
able). It would wrap around to the lowest acceptable value, which is 128, and start
counting upward from there. Overflow isn't something you can readily deal with in
a program, so be sure to give your variables plenty of living space in their chosen
data type.
Q Why does Java have all these shorthand operators for arithmetic and assign-
ment? It's really hard to read that way.
A Java's syntax is based on C++, which is based on C (more Russian nesting doll
behavior). C is an expert language that values programming power over readability,
and the shorthand operators are one of the legacies of that design priority. Using
them in a program isn't required because effective substitutes are available, so you
can avoid them in your own programming, if you prefer.
 
 
Search WWH ::




Custom Search