Operators in Java: Arithmetic, Assignment, Relational, Logical, Ternary and Bitwise Operators
Operators are special symbols used to perform operations on values and variables in Java.
For example:
int a = 4;
int b = 3;
System.out.println(a + b);
Here, + is an operator that performs addition.
Operators are fundamental to Java programming because they are used for calculations, assigning values, comparing data, creating conditions, making decisions, and performing operations at the bit level.
In this lesson, we will learn:
-
Arithmetic operators
-
Assignment operators
-
Relational operators
-
Logical operators
-
Ternary operator
-
Type cast operator
-
Bitwise operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Java provides these commonly used arithmetic operators:
| Operator | Operation |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo |
Let's look at an example:
int a = 4;
int b = 3;
System.out.println(a + b); // 7
System.out.println(a - b); // 1
System.out.println(a * b); // 12
System.out.println(a / b); // 1
The first three operations behave as expected:
4 + 3 = 7
4 - 3 = 1
4 × 3 = 12
But notice:
4 / 3 = 1
Why don't we get 1.333...?
Because both a and b are int. When integer values are divided using integer division, the fractional portion is discarded.
Integer Division vs Floating-Point Division
Consider:
float a = 4;
float b = 3;
System.out.println(a / b);
The output is approximately:
1.3333334
Because the operands are floating-point values, the result can contain a fractional part.
This distinction is important when writing calculations involving prices, measurements, percentages, averages, and other decimal values.
2. Modulo Operator %
The modulo operator is represented by:
%
It returns the remainder left after division.
For example:
System.out.println(10 % 4);
Let's calculate:
10 ÷ 4
4 × 2 = 8
10 - 8 = 2
Therefore:
10 % 4 = 2
Another example:
int x = 113;
int y = 3;
System.out.println(x % y);
Since:
3 × 37 = 111
the remainder is:
2
Therefore, the output is:
2
The modulo operator is extremely useful in programming. For example, checking whether a number is even can be done using:
int number = 10;
System.out.println(number % 2 == 0);
If the remainder is 0, the number is divisible by 2.
3. Assignment Operators
Assignment operators are used to assign values to variables.
The basic assignment operator is:
=
For example:
int a = 10;
Here, 10 is assigned to a.
Java also provides compound assignment operators:
+=
-=
*=
/=
For example:
int z = 3;
z += 3;
This is equivalent to:
z = z + 3;
Therefore, z becomes:
6
Similarly:
z -= 3;
means:
z = z - 3;
And:
z *= 3;
means:
z = z * 3;
While:
z /= 3;
means:
z = z / 3;
Compound assignment operators make it easier to update an existing variable.
4. Relational Operators
Relational operators are used to compare two values.
They return a Boolean result:
true
or:
false
Java provides:
| Operator | Meaning |
|---|---|
< |
Less than |
> |
Greater than |
== |
Equal to |
!= |
Not equal to |
>= |
Greater than or equal to |
<= |
Less than or equal to |
Example:
int m = 4;
int n = 3;
System.out.println(m < n); // false
System.out.println(m > n); // true
System.out.println(m == n); // false
System.out.println(m != n); // true
System.out.println(m >= n); // true
System.out.println(m <= n); // false
These operators become particularly important when we start working with conditional statements such as if, else if, and while.
= vs ==
One of the most common mistakes beginners make is confusing:
=
with:
==
They have completely different purposes.
Assignment
int a = 10;
= assigns a value.
Comparison
a == 10
== checks whether two values are equal.
Remember:
=means assignment, while==means comparison.
5. Logical Operators
Logical operators are used to combine multiple Boolean expressions.
The two operators covered here are:
&&
||
Logical AND &&
The AND operator returns true only when all conditions are true.
For example:
System.out.println((4 < 5) && (10 < 11));
Both conditions are true:
true && true
Therefore:
true
Now consider:
System.out.println((14 < 11) && (10 < 11));
This becomes:
false && true
Therefore, the result is:
false
Logical OR ||
The OR operator returns true if at least one condition is true.
For example:
System.out.println(
(4 > 2) || (55 > 12) || (5 > 1)
);
All three conditions are true, so the result is:
true
Even if only one condition is true, OR still returns true:
System.out.println(
(4 < 2) || (55 < 12) || (5 > 1)
);
This becomes:
false || false || true
Therefore:
true
Logical operators are essential for creating complex conditions in Java programs.
6. Ternary Operator
The ternary operator is a compact way of writing a simple conditional expression.
Its syntax is:
condition ? expression_if_true : expression_if_false
For example:
int g = 34;
int h = 23;
System.out.println(
g > h ? "Your statement is true" : "Your statement is false"
);
The condition is:
34 > 23
which is true.
Therefore, Java selects:
Your statement is true
If the condition were false, Java would select the expression after :.
The ternary operator is useful when a simple if-else decision needs to produce a value.
7. Type Cast Operator
The type cast operator is represented using parentheses containing the target data type:
(type)
For example:
int q = 34;
byte w = (byte) q;
Here:
(byte)
is the explicit type cast operator.
It tells Java to convert the value of q into a byte.
Type casting was covered in detail in the previous lesson, especially when converting between primitive data types.
8. Bitwise Operators
Bitwise operators work directly with the individual bits of integer values.
One of the operators introduced in this lesson is:
&
which represents Bitwise AND.
Let's take:
int r = 12;
int p = 8;
System.out.println(r & p);
First, represent the values in binary:
12 = 1100
8 = 1000
Now perform AND on each corresponding bit:
1100
& 1000
------
1000
The rule for AND is simple:
1 & 1 = 1
Every other combination produces:
0
Therefore:
1100 & 1000 = 1000
Binary 1000 represents decimal 8.
So:
System.out.println(r & p);
produces:
8
Bitwise operators are particularly useful in areas such as bit manipulation, low-level programming, flags, masks, permissions, and performance-oriented operations. We will explore them in much greater detail when studying Bit Manipulation in Java.
9. Quick Summary of Java Operators
| Category | Operators | Purpose |
|---|---|---|
| Arithmetic | + - * / % |
Mathematical operations |
| Assignment | = += -= *= /= |
Assign/update values |
| Relational | < > == != >= <= |
Compare values |
| Logical | && || ! |
Evaluate Boolean conditions |
| Ternary | ? : |
Compact conditional expression |
| Type Cast | (type) |
Explicit type conversion |
| Bitwise | & | |
Operate on individual bits |
Common Beginner Mistakes
While working with Java operators, beginners commonly make a few mistakes.
1. Confusing / with %
10 / 3
gives the quotient for integer operands, while:
10 % 3
gives the remainder.
2. Confusing = and ==
a = 10;
assigns a value.
a == 10
compares values.
3. Expecting decimal results from integer division
int a = 4;
int b = 3;
System.out.println(a / b);
produces:
1
not 1.333....
4. Confusing & and &&
These are different operators.
& → Bitwise AND
&& → Logical AND
& works at the bit level, whereas && works with Boolean conditions.
Final Takeaway
Operators are one of the fundamental building blocks of Java programming. They allow us to perform calculations, assign values, compare data, combine conditions, make compact decisions, convert data types, and manipulate individual bits.
The most important operators introduced in this lesson are:
Arithmetic → + - * / %
Assignment → = += -= *= /=
Relational → < > == != >= <=
Logical → && ||
Ternary → ? :
Type Cast → (type)
Bitwise → & |
Understanding these operators thoroughly will make upcoming topics such as conditional statements, loops, expressions, and operator precedence much easier.
In the next lesson, we will learn Operator Precedence and understand why an expression such as:
a + b - c * d
does not simply execute from left to right. We will learn which operator Java executes first and how precedence rules determine the final result.