Comprog1 W Module 4
Comprog1 W Module 4
PROGRAMMING
WEEK 4 - MODULE
Operators
In Java, there are different types of operators. There are arithmetic operators, relational
operators, logical operators and conditional operators. These operators follow a certain kind of
precedence so that the compiler will know which operator to evaluate first in case multiple
operators are used in one statement.
Arithmetic operators
Here are the basic arithmetic operators that can be used in creating your Java
programs,
Note: When an integer and a floating-point number are used as operands to a single
arithmetic operation, the result is a floating point. The integer is implicitly converted to a
floating-point number before the operation takes place.
For example,
int i = 10,
int j = 3;
int k = 0;
When the increment and decrement operators are placed after the operand, the old value
of the variable will be used in the expression where it appears.
For example,
int i = 10,
int j = 3;
int k = 0;
Relational operators
Relational operators compare two values and determines the relationship between
those values. The output of evaluation are the boolean values true or false.
Logical operators
Logical operators have one or two boolean operands that yield a boolean result.
There are six logical operators: && (logical AND), & (boolean logical AND), || (logical
OR), | (boolean logical inclusive OR), ^ (boolean logical exclusive OR), and ! (logical
NOT).
The basic difference between && and & operators is that && supports short-
circuit evaluations (or partial evaluations), while & doesn't. What does this mean?
Given an expression,
exp1 && exp2
&& will evaluate the expression exp1, and immediately return a false
value is exp1 is false. If exp1 is false, the operator never evaluates exp2 because
the result of the operator will be false regardless of the value of exp2. In contrast,
the & operator always evaluates both exp1 and exp2 before returning an answer.
Here's a sample source code that uses logical and boolean AND,
Note, that the j++ on the line containing the && operator is not evaluated since
the first expression (i>10) is already equal to false.
Given an expression,
exp1 || exp2
|| will evaluate the expression exp1, and immediately return a true value is exp1 is
true. If exp1 is true, the operator never evaluates exp2 because the result of the operator
will be true regardless of the value of exp2. In contrast, the | operator always evaluates
both exp1 and exp2 before returning an answer.
Here's a sample source code that uses logical and boolean OR,
public class TestOR {
public static void main( String[] args ) {
int i = 0;
int j = 10;
boolean test= false;
//demonstrate ||
test = (i < 10) || (j++ > 9);
System.out.println(i);
System.out.println(j);
System.out.println(test);
//demonstrate |
test = (i < 10) | (j++ > 9);
System.out.println(i);
System.out.println(j);
System.out.println(test);
}
}
Here's a sample source code that uses the logical exclusive OR operator,
public class TestXOR {
public static void main( String[] args ) {
boolean val1 = true;
boolean val2 = true;
System.out.println(val1 ^ val2);
val1 = false;
val2 = true;
System.out.println(val1 ^ val2);
val1 = false;
val2 = false;
System.out.println(val1 ^ val2);
val1 = true;
val2 = false;
System.out.println(val1 ^ val2);
}
}
! (logical NOT)
The logical NOT takes in one argument, wherein that argument can be an
expression, variable or constant.
Here is the truth table for !,
Here's a sample source code that uses the logical NOT operator,
Operator Precedence
Operator precedence defines the compiler’s order of evaluation of operators so as
to come up with an unambiguous result.
we can re-write the expression and place some parenthesis base on operator
precedence,
((6%2)*5)+(4/2)+88-10;
References:
JEDI Course Notes