You are on page 1of 21

Java Operators

Module Overview
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, 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?
Operators and Their Precedence (cont.)
• 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.
Assignment Operator
• Assignment operator is represented by a single equal sign
(=).
• The syntax in using this operator is:
<variable> = <expression>;

Examples:
Assignment Operator (cont.)
• 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:
Assignment Operator (cont.)
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.
Arithmetic Operators (cont.)
• 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:
Arithmetic Operators
(cont.)
• 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.
Unary Operator (cont.)
• Let’s see these unary operators
in action:
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 at the right
summarizes the equality
and relational operators.
Equality and Relational Operators (cont.)
• 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.
Conditional Operators
• 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 at the right lists
the different conditional
operators in Java.
Conditional Operators (cont.)
• Conditional AND is depicted with two ampersand (&&)
symbols. The result of this operator will only be TRUE if and
only if BOTH values are TRUE.
• Here’s the truth table for conditional AND for better explanation.
• Let’s assume you have two Boolean variables – varA and varB.
Conditional Operators (cont.)
• The Conditional OR 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.
Conditional Operators (cont.)
• Take this for example:
Conditional Operators (cont.)
• The ternary operator 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:
Conditional Operators (cont.)
• Try and write it on Netbeans:
References
• Oracle. Java™ Platform Standard Ed. 7. Retrieved from:
https://docs.oracle.com/javase/tutorial/java/

You might also like