04 Basic Operators and Operator Precedence
04 Basic Operators and Operator 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.
04 Handout 1 *Property of STI
Page 1 of 3
IT1708
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:
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.