Chapter 13 of 57

Operators in Java

When writing programs, we often need to perform operations on values. We may want to add two numbers, compare two values, check whether something is true, or increase a number by one.

For example:

int a = 10;
int b = 5;

System.out.println(a + b);

Here, + tells Java to add the two values. This symbol is called an operator.

In simple words, an operator is a symbol that tells Java to perform a particular operation.

Types of Operators in Java

Java provides several types of operators. The most commonly used ones are:

  • Arithmetic operators

  • Assignment operators

  • Comparison operators

  • Logical operators

  • Unary operators

  • Bitwise operators

  • Ternary operator

Let's understand them one by one.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

The main arithmetic operators are:

Operator

Meaning

Example

+

Addition

10 + 5

-

Subtraction

10 - 5

*

Multiplication

10 * 5

/

Division

10 / 5

%

Remainder

10 % 3

For example:

int a = 10;
int b = 3;

System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);

Output:

13
7
30
3
1

Notice that 10 / 3 gives 3, not 3.33. This happens because both values are integers, so Java performs integer division.

The % operator gives us the remainder. For example, 10 % 3 gives 1 because 1 is left after dividing 10 by 3.

Assignment Operators

Assignment operators are used to assign values to variables.

The most basic assignment operator is =.

int age = 25;

Here, = assigns 25 to age.

Java also provides shorthand assignment operators:

int number = 10;

number += 5;   // number = number + 5
number -= 2;   // number = number - 2
number *= 3;   // number = number * 3
number /= 2;   // number = number / 2

For example:

int score = 10;

score += 5;

System.out.println(score);

Output:

15

So instead of writing:

score = score + 5;

we can simply write:

score += 5;

Comparison Operators

Comparison operators are used to compare two values.

They produce a boolean result: either true or false.

The main comparison operators are:

Operator

Meaning

==

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

For example:

int age = 25;

System.out.println(age == 25);
System.out.println(age > 18);
System.out.println(age < 18);

Output:

true
true
false

One important thing to remember is that = and == are different.

age = 25;

means assign 25 to age.

While:

age == 25

means check whether age is equal to 25.

This is a very common beginner mistake.

Logical Operators

Logical operators are used when we want to combine or modify conditions.

Java provides three main logical operators:

Operator

Meaning

&&

AND

`

!

NOT

AND &&

The && operator returns true only when both conditions are true.

int age = 25;

System.out.println(age >= 18 && age <= 60);

Both conditions are true, so the result is:

true

OR ||

The || operator returns true when at least one condition is true.

int age = 25;

System.out.println(age < 18 || age > 20);

The second condition is true, so the result is:

true

NOT !

The ! operator reverses a boolean value.

boolean isLoggedIn = true;

System.out.println(!isLoggedIn);

Output:

false

Because !true becomes false.

Unary Operators

Unary operators work with one value.

Some common unary operators are ++, --, +, -, and !.

The most common ones for beginners are increment and decrement.

int number = 10;

number++;
System.out.println(number);

Output:

11

++ increases the value by 1.

Similarly:

int number = 10;

number--;
System.out.println(number);

Output:

9

-- decreases the value by 1.

We will explore the difference between prefix and postfix ++ and -- when we discuss them in more detail.

Ternary Operator

The ternary operator is a short way of writing a simple conditional expression.

Its syntax is:

condition ? valueIfTrue : valueIfFalse

For example:

int age = 20;

String result = age >= 18 ? "Adult" : "Minor";

System.out.println(result);

Output:

Adult

Here, Java checks whether age >= 18 is true. If it is true, "Adult" is selected. Otherwise, "Minor" is selected.

The ternary operator can be useful when you have a simple condition and don't want to write a full if-else statement.

Operator Precedence

Sometimes an expression contains multiple operators:

int result = 10 + 5 * 2;

You might expect Java to calculate 10 + 5 first, but Java follows operator precedence.

Multiplication has higher precedence than addition, so Java calculates:

5 × 2 = 10

and then:

10 + 10 = 20

Therefore:

System.out.println(10 + 5 * 2);

produces:

20

If you want addition to happen first, use parentheses:

System.out.println((10 + 5) * 2);

Now the result is:

30

Parentheses are a simple way to make the order of calculation clear.

A Simple Example

Let's use several operators in one program:

class Main {
    public static void main(String[] args) {

        int marks = 85;

        System.out.println("Marks: " + marks);
        System.out.println("Passed: " + (marks >= 40));
        System.out.println("Marks after bonus: " + (marks + 5));
    }
}

Output:

Marks: 85
Passed: true
Marks after bonus: 90

Here, we used the + operator for addition and joining text, while >= was used to compare the marks with 40.

Operators are used throughout Java programs, so becoming comfortable with them is important. As you learn conditions, loops, and other programming concepts, you'll be using these operators constantly.