There are five arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% mod (modulus) operator
You can use these operators with both integral and floating-point data types. Integral division truncates any fractional part; there is no rounding.
An arithmetic expression is constructed by using arithmetic operators and numbers. The numbers in the expression are called operands. Moreover, the numbers used to evaluate an operator are called the operands for that operator. Operators that have only one operand are called unary operators. Operators that have two operands are called binary operators.
Order of Precedence
Java uses operator precedence rules to determine the order in which operations are performed to evaluate the expression. According to the order of precedence rules for arithmetic operators, *, /, and % have a higher level of precedence than + and -.
Because arithmetic operators are evaluated from left to right, unless parentheses are present, the associativity of arithmetic operators is said to be from left to right.
Since the char data type is also an integral data type, Java allows you to perform arithmetic operations on char data.
Expressions
If all operands in an expression are integers, the expression is called an integral expression. If all operands in an expression are floating-point numbers, the expression is called a floating-point or decimal expression.
Evaluating an integral or a floating-point expression is straightforward. When operators have the same precedence, the expression is evaluated from left to right. To avoid confusion you can always use parentheses to group operands and operators.
No comments:
Post a Comment