You are on page 1of 9

Programming NC IV

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.

Operators and Their Precedence


Operators, what are these things? Operators, in Java programming, are
special symbols or characters which are used to perform specific operations
on one or more operand which return a result. In short, they are used to
manipulate the primitive data types that we have discussed.
Operators, like our society, have what they call “precedence” or the order of
importance. Imagine using dozens of operators – which are you going to
execute first?
You have probably heard of MDAS, right? Multiplication first, before division
followed by addition and subtraction. That’s what operator precedence is all
about.
In Java, operator precedence follows this order, that is, the MDAS rule. The
closer an operator is at the top, the higher its precedence is.

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 = += -= *= /= &= ^= |= <<= >>= >>>=

Assignment, Arithmetic and Unary Operators


Assignment Operator
No, dude. We’re not talking about homework here. Assignment operator, in
Java programming language, is represented by a single equal sign (=).
The syntax in using this operator is:
<variable> = <expression>;
Examples:
int score1 = 90;
int score2 = 99;

In the example above, 90 is assigned to score1 while 99 is assigned to


score2.
In other words, the = operators assigns the value on its right to the operand
on its left.
If we are going to print their values, then we’ll need to write the code like:
int score1 = 90;
int score2 = 99;

System.out.println(“Score 1 = ” + score1);
System.out.println(“Score 2 = ” + score2);

This operator can also be used in assigning object references to objects –


which will be discussed in a later module.
From time to time, you might also encounter this type of statement:
x += z; // equal to x = x + z;
x -= z; // equal to x = x – z;
x /= z; // equal to x = x / z;
x *= z; // equal to x = x * z;
x %= z; // equal to x = x % z;

Thus giving us additional assignment operators as shown in the table below.


Programming NC IV
3
Java Operators

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:

Figure 1. Arithmetic Operators

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

Let’s see these unary operators in action:

Figure 2. Unary 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

Pretty easy, don’t you reckon?


Just remember, these statements are different:
x = 9;
x == 10;
The first one is assigning or storing 9 to variable x.
The latter is comparing if x is equal to 10.
Programming NC IV
7
Java Operators

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

Contrary to conditional AND, conditional OR will only be FALSE if BOTH of the


expressions are FALSE.
Take this code snippet for example:
boolean result;
result = (5 > 9) || (8==10); //false
result = (10 >= 9) || (-7 < 2); //true
result = (7 != 0) || (43 >= 44); //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);

The first line is FALSE and the second is TRUE.


See for yourself:

Figure 3. Conditional Operators


Programming NC IV
9
Java Operators

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;

result = (7 < x) ? true : false;


result = (7 < x && 1 != 10) ? true : false;
answer = (7 < x && 1 != 10) ? 10 : 100;

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:

Figure 4. Ternary Operators

References
Oracle. Java™ Platform Standard Ed. 7. Retrieved from:
https://docs.oracle.com/javase/tutorial/java/

Course Module

You might also like