COMPROG 1 - BASIC OPERATORS AND PRECEDENCE
Operators
Operators – these are specific symbols in a programming language. These symbols tell the compiler or interpreter of the program to perform a specific mathematical, relational, or logical operation and produce the final result.
Arithmetic Operators
Arithmetic operators – are used to perform basic mathematical operations on numerical values. A value used on either side of an operator is an operand. For example, in the expression 45 + 2, the numbers 45 and 2 are operands. Table 1 shows the list of arithmetic operators.
Table 1: Arithmetic operators (Baesens, Backiel, & Broucke, 2015)
Assume the following variable declarations: int x = 11, y = 3;
Operator | Description | Example | Return value |
+ | Addition | x + y | 14 |
- | Subtraction | x – y | 8 |
* | Multiplication | x * y | 33 |
/ | Division | x / y | 3 (not 3.66) |
% | Modulus | x % y | 2 |
The operators (/) and (%) have special considerations. Java supports two (2) types of division:
- Floating-point division occurs when either or both of the operands are floating-point values and the result is a floating-point value. For example: int x = 11; double y = 3; System.out.println( x / y );
//the output is 3.66
- Integer division occurs when both of the operands are integers. The result is an integer, and any factorial part of the result is lost. For example: int x = 11; int y = 3; System.out.println( x / y ); //the output is 3
Relational Operators
Relational operators are used to evaluate the relation between the operands and generate a decision on that base. These typically return a Boolean value. For example, a relational operator is used to compare the scores of two (2) students (score1
> score2) or (score1 < score2). Table 2 illustrates the relational operators used in Java.
Table 2: Relational operators (Baesens, Backiel, & Broucke, 2015)
Assume the following variable declarations: int x = 11, y = 3;
Operator | Description | Example | Return value |
> | Greater than | x > y | true |
>= | Greater than or equal | x >= y | true |
< | Lesser than | x < y | false |
<= | Lesser than or equal | x <= y | false |
== | Equal | x == y | false |
!= | Not equal | x != y | true |
The relational expressions can also be used to assign value in variables. For example:
boolean b = 10 > 2; //the expression 10 > 2 is evaluated and the result is true and assigned to the variable b
Logical Operators
Logical operators – return a Boolean value based on the Boolean result of the given expressions. Logical operators are always evaluated from left to right. For example, the expression (3 > 2) && (2 < 3) has a return value of true. The relational expressions were evaluated first, and then its Boolean results are used to evaluate the logical expression with logical AND (&&) operator.
Table 3 illustrates the evaluation of logical operators used in Java.
Table 3: Logical operators (Baesens, Backiel, & Broucke, 2015)
Assume the following variable declarations: boolean A = 3 > 2; boolean B = 2 < 1;. The value of Boolean variable
A as the expression is evaluated is true and variable B is false.
Operator | Description | Example | Return value |
&& | Logical AND operator: If both operands are true, then the condition becomes true | A && B | true |
|| | Logical OR operator: If at least one (1) operand is true, then the condition becomes true | A || B | true |
^ | Bitwise and Logical XOR operator: If only one (1) operand is true, then the condition becomes true | A ^ B | true |
! | Unary NOT operator: Use to reverse the logical state of its operand. If the condition is true, then the Logical NOT operator will make it false | !A | false |
!(A && B) | true | ||
A && !B | true |
Assignment Operators
Assignment operators – these are used to assign values to a variable. The left operand gets the value of the expression on the right. For example, int x = 20;, the variable x gets 20.
Table 4 lists some of the assignment operators used in Java.
Table 4: Assignment operators (Baesens, Backiel, & Broucke, 2015)
Assume the following variable declarations: int score;
Operator | Description | Example | Return value |
= | Assigns values from right side operands to the left side operands | score = 32 | 32 |
+= | Adds right operand to the left operand and assigns the result to the left operand | score+=2 | 34 |
Same as score = score + 2 | |||
-= | Subtracts right operand from left operand and assigns the result to the left operand | score-=3 | 31 |
score = score – 3 | |||
*= | Multiplies right operand with the left operand and assigns the result to the left operand | score*=2 | 62 |
score = score * 2 | |||
/= | Divides left operand with the right operand and assigns the result to the left operand | score/=2 | 31 |
score = score / 2 | |||
%= | It takes modulus using two (2) operands and assigns the result to the left operand | score%=2 | 1 |
score = score % 2 | |||
++ | Adds 1 to the left operand and assigns the result to the left operand | score++ | 2 |
score = score + 1 | |||
-- | Subtracts 1 from the left operand and assign the result to the left operand | score-- | 1 |
score = score – 1 |
Order of Precedence
Order of precedence – it is a collection of rules that specifies which operations need to be performed in an expression. For a given expression containing more than two (2) operators, it determines which operations should be calculated first.
Certain operators have higher precedence than others. For example, the multiplication operator has higher precedence than the addition operator: int x = 10 + 3 * 8. In this expression, variable x is assigned 34 because the operator (*) has higher precedence than operator (+), so 3 gets multiplied by 8 first, and then 10 is added to the product. Table 5 lists the operators used in Java from highest to lowest precedence.
Table 5: Operators precedence (Farrell, 2014)
Precedence | Operators | Symbol |
Highest | Unary NOT | ! |
Intermediate | Multiplication, division, modulus | *, /, % |
Addition, subtraction | +, - | |
Relational | >, <, >=, <= | |
Equality | ==, != | |
Logical AND | && | |
Logical OR | || |
Lowest | Assignment | = |
When operators of equal precedence appear in the same expression, a rule must govern, which is evaluated first. In the expression, all binary operators, except assignment operators, are evaluated from left to right. To avoid confusion, programmers used parentheses to group expressions.
Precedence rules can be overridden by explicit parentheses (). These can be used to group items in an expression. Parentheses are used to tell the computer which operations to perform first. For example, consider the first and second statements with different positioning of their parentheses:
int a = 10, b = 3, c = 8, ex1, ex2;
ex1 = (a + b) * c; //example 1, result is 104 ex2 = a + (b * c); //example 2, result is 34
To evaluate the expression in the second statement, the computer first adds variables a and b and then multiplies the result by variable c. To evaluate the expression in the third statement, it multiplies variables b and c and then adds the result to variable a. When these two (2) expressions are evaluated, they produce different results.
REFERENCES:
Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning java programming: The object-oriented approach. Indiana: John Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th edition. Boston: Course Technology, Cengage Learning.
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th edition. California: Pearson Education, Inc.