Week 013-014 Module Java Operators
Week 013-014 Module Java Operators
1
Java Operators
Java Operators
This module discusses what a Java operator is and when it is used. Different
types of operators such as assignment, arithmetic and unary operators will
be covered. Using equality, relational and conditional operators in comparing
values and evaluating Boolean expressions will be discussed as well.
After this module, students should be able to:
1. learn the fundamental concepts of Java operators;
2. use assignment, arithmetic and unary operators appropriately in writing
a Java program; and
3. utilize equality, relational and conditional operators in Java.
Operator Precedence
Postfix counter++ counter--
Unary ++counter –counter +counter – counter ~ !
Multiplicative * / %
Additive + -
Shift << >> >>>
Relational < > <= >= instanceof
Equality == !=
Bitwise AND &
Bitwise Exclusive OR ^
Bitwise Inclusive OR |
Course Module
Logical AND &&
Logical OR ||
Ternary ?:
Assignment = += -= *= /= &= ^= |= <<= >>= >>>=
System.out.println(“Score 1 = ” + score1);
System.out.println(“Score 2 = ” + score2);
Operator Description
+= Addition assignment operator.
Adds the right and left operand and assigns the
sum to the left operand.
-= Subtraction assignment operator.
Subtracts the right from left operand and assigns
the difference to the left operand.
*= Multiplication assignment operator.
Multiplies the right and left operand and assigns
the product to the left operand.
/= Division assignment operator.
Divides the left with the right operand and assigns
the quotient to the left operand.
%= Modulus assignment operator.
Takes the remainder of dividing the right and left
operand and assigns the result to the left operand.
Arithmetic Operators
Arithmetic operators allow you to perform addition, subtraction,
multiplication and division in Java which are very similar to basic
mathematics. The table below lists the Java arithmetic operators.
Operator Description
+ Additive operator
Also used in concatenating strings
- Subtractive operator
* Multiplicative operator
/ Division operator
% Remainder operator
Wait a minute. What’s that percentage (%) operator? It’s called modulus
operator which divides two numbers and, instead of returning the quotient,
returns the remainder.
For example:
int x = 2 % 2; // x is equal to 0, 0 is the remainder
int y = 2 / 2; // y is equal to 1, 1 is the quotient
int z = 6 % 4; // z is equal to 2, 2 is the remainder
int a = 6 / 4; // a is equal to 1, 1 is the quotient
Course Module
Let’s try this in a simple demonstration program:
Unary Operator
Unary operators, from the name itself, uses only one operand. Through this ,
operators are used in incrementing, decrementing, negating or inverting the
value of an operand.
The following are the Java unary operators.
Operator Description
+ Unary plus operator; positive value
(By default, even numbers without this
operator is positive)
- Unary minus operator; negates an
expression
++ Increment operator; increases value by 1
-- Decrement operator; reduces value by 1
! Logical complement operator
When used with a Boolean variable, its value
is inverted
Programming NC IV
5
Java Operators
As you can see, number is set to 7 by default. By using the minus operator, it
became -7.
Then, we changed its value to +1 by writing the code number = +1. Simple
as that.
Next, we incremented it by 1 by using number++; statement. It basically
means number = number + 1.
Then, we decremented it by 1 again through number-- or in other words
number = number – 1;
We then experimented with the Boolean value of isSuccessful. It was
declared with the value true.
But when we printed it as !isSuccessful, true becomes false; because
!true = false or simply, not true is false.
Easy, right?
Course Module
Equality, Relational and Conditional Operators
Equality and Relational Operators
Equality and relational operators test whether two values are equal or
unequal. In other words, they find out which is greater or less of the two
values.
The table below summarizes the equality and relational operators.
Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
As you can see, we have double equal sign (==) to evaluate if two operands
are equal.
Keep in mind that it is DIFFERENT from the single equal sign (=) assignment
operator.
Let’s see some examples for these operators.
int x = 9;
int z = 7;
boolean result;
result = x == z; // false
result = x != 68; // true
result = x > z; // true
result = z < 10; // true
result = x >= 9; // true
result = z <= 6; // false
result = z > (2*3); // true
result = (z + x) != 16; //false
Conditional Operators
Consider this conversation:
“Okay, fine. I will buy you a new laptop, in one condition.”
“What?”
“You should get perfect score in Java programming final exam.”
“Deal!”
What will happen if Person B gets a perfect score in Java? New laptop! Right,
you have to get that 100% score for your new gadget – that’s the condition.
In Java programming, we also have conditional operators. These operators
are also known as logical operators. They are used to evaluate Boolean
variables as well.
The table below lists the different conditional operators in Java.
Operator Description
&& Conditional AND
|| Conditional OR
?: Ternary
Shorthand for the if-then-else
statement
Let’s discuss Conditional AND first. It is depicted with two ampersand (&&)
symbols. The result of this operator will only be TRUE if and only if BOTH
values are TRUE. Again?
Okay, here’s the truth table for conditional AND for better explanation. Let’s
assume you have two Boolean variables – varA and varB.
varA varB varA && varB
true false False
false true False
false false False
true true True
See what I meant? All variables should be TRUE for the result of conditional
AND to be TRUE. If one or both of them is FALSE, then the result will be FALSE.
Let’s take a look at this code snippet:
boolean result;
result = (5 > 9) && (8==10); //false
result = (10 >= 9) && (-7 < 2); //true
result = (7 != 0) && (43 >= 44); //false
Course Module
Now, the Conditional OR. This operator uses two pipe symbols (||). You can
find this symbol above the Enter key on your keyboard.
The result of this operator is TRUE if at least one of the values is TRUE.
Here’s the conditional OR truth table for demonstration.
varA varB varA || varB
true false true
false true true
false false false
true true true
With the last line of the code, only 7 != 0 is true, but the result is still true
since one of the two expressions is true.
But can there be more than one expression?
Yes! Take this for example:
boolean result;
result = (5 > 9) || (8==10) || (1 != 1);
result = (10 >= 9) && (-7 < 2) && (99 <= 100);
And last, but not the least, is the ternary operator (?:).
This is known as the shorthand for the if-then-else statement (this will
be discussed in a later module).
The syntax in using this operator is:
<variable> = <condition> ? <valueIfTrue> : <valueIfFalse>
Examples:
int x = 7;
int answer;
boolean result;
The ternary operator uses a condition, question mark, value if the condition
is met, colon and value if the condition is false.
Basically, if the condition is met, the value if true will be assigned to the
operand at the left; or else, the value if false will be assigned.
Observe the example above. The condition is 7 < x, we know it’s false
since x=7; therefore, the value of result is true.
And the last line of the code, (7 < x && 1!=10), it’s false so the value of
answer is 100.
Try and write it on Netbeans:
References
Oracle. Java™ Platform Standard Ed. 7. Retrieved from:
https://docs.oracle.com/javase/tutorial/java/
Course Module