Operators
It's useful to divide Java's operators into these categories: arithmetic, relational and conditional, bitwise and logical, and assignment. These are discussed in the following sections.
Arithmetic Operators
The Java language supports various arithmetic operators for all floating-point and integer numbers. These include + (addition), - (subtraction), * (multiplication), / (division), and % (modulo).
The precedence that applies when an expression using these operators is evaluated is same as you learned in school. Multiplication and division are executed before any addition or subtraction operations.
For example,
15 – 2*5 + 20/5
will produce the value 9, since it is equivalent to 15 – 10 + 4.
If you want to change the sequence of operations then you can use parentheses. Expressions within parentheses are always evaluated first, starting with the innermost when they are nested.
For example,
(15 – 2) * (5 + 20) / 5
will produce 65.
No comments:
Post a Comment