Paramclasses Academy - Blog

Tutorials Details

Java Operators and Expressions: A Complete Beginner's Guide

Java Operators and Expressions: A Complete Beginner's Guide

October 21, 2026

Introduction

Operators are an essential part of Java programming. They allow developers to perform calculations, compare values, assign data and make decisions.

For example, a calculator uses operators to add, subtract, multiply and divide numbers. Java programs use similar operators to perform calculations and process information.

In this tutorial, you will learn the main types of Java operators, how expressions work, and how to use them in practical programs.

What Are Operators in Java?

An operator is a symbol that performs an operation on one or more operands.

For example:

int result = 10 + 5;
System.out.println(result);

Output:

15

Here:

  • 10 and 5 are operands.

  • + is the arithmetic operator.

  • 10 + 5 is an expression.

  • result stores the calculated value.

What Is an Expression in Java?

An expression is a combination of variables, values, operators and sometimes method calls that evaluates to a value.

Example:

int a = 10;
int b = 5;

int result = a * b + 2;

System.out.println(result);

Output:

52

Java evaluates multiplication before addition, so the result is (10 * 5) + 2.

Types of Operators in Java

Java provides several categories of operators:

  1. Arithmetic operators

  2. Unary operators

  3. Assignment operators

  4. Relational operators

  5. Logical operators

  6. Bitwise operators

  7. Shift operators

  8. Ternary operator

  9. instanceof operator

Let's understand each category with examples.

1. Arithmetic Operators in Java

Arithmetic operators perform mathematical calculations.

Operator Meaning Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Remainder 10 % 3 1

Example Program

public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;

        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
        System.out.println("Remainder: " + (a % b));
    }
}

Output:

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Remainder: 1

Important: When both operands are integers, division discards the fractional part. For example, 10 / 3 produces 3, not 3.33.

To get a decimal result, use a floating-point operand:

System.out.println(10.0 / 3);

Output: approximately 3.3333333333333335.

2. Unary Operators in Java

Unary operators operate on a single operand.

Common unary operators include:

  • + — indicates a positive value.

  • - — negates a numeric value.

  • ++ — increments a variable by one.

  • -- — decrements a variable by one.

  • ! — reverses a Boolean value.

  • ~ — inverts the bits of an integer value.

Increment and Decrement Example

int a = 5;

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

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

Output:

6
5

Difference Between a++ and ++a

Both forms increase the variable by one. The difference matters when the expression's value is used.

int a = 5;
int x = a++;

System.out.println(x);
System.out.println(a);

Output:

5
6

In postfix form (a++), the expression uses the original value before incrementing.

Now consider prefix form:

int a = 5;
int x = ++a;

System.out.println(x);
System.out.println(a);

Output:

6
6

In prefix form (++a), the variable is incremented before its value is used.

3. Assignment Operators in Java

Assignment operators assign values to variables.

Operator Example Equivalent Form
= a = 10 Assigns 10 to a
+= a += 5 a = a + 5
-= a -= 5 a = a - 5
*= a *= 2 a = a * 2
/= a /= 2 a = a / 2
%= a %= 3 a = a % 3

Example

int a = 10;

a += 5;
System.out.println(a);

a *= 2;
System.out.println(a);

Output:

15
30

The first statement increases a from 10 to 15. The second multiplies its current value by 2.

4. Relational Operators in Java

Relational operators compare two values and return a Boolean result: true or false.

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example

int a = 10;
int b = 5;

System.out.println(a > b);
System.out.println(a == b);
System.out.println(a != b);

Output:

true
false
true

Important: = assigns a value, whereas == compares values.

int age = 20;          // Assignment
System.out.println(age == 20); // Comparison

For objects such as String, == compares references rather than text content. Use equals() when you want to compare String contents.

5. Logical Operators in Java

Logical operators combine or reverse Boolean conditions.

Operator Meaning
&& Logical AND
`   ` Logical OR
! Logical NOT

Logical AND (&&)

Returns true only when both conditions are true.

int age = 20;

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

Output: true

Logical OR (||)

Returns true when at least one condition is true.

int marks = 35;

System.out.println(marks < 33 || marks >= 35);

Output: true

Logical NOT (!)

Reverses a Boolean value.

boolean isRaining = false;

System.out.println(!isRaining);

Output: true

Short-circuit evaluation: With &&, Java skips the second condition if the first is false. With ||, Java skips the second condition if the first is true.

6. Bitwise Operators in Java

Bitwise operators work on individual bits of integral values.

Operator Meaning
& Bitwise AND
` ` Bitwise OR
^ Bitwise XOR
~ Bitwise complement

Example:

int a = 5;
int b = 3;

System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);

Output:

1
7
6

Why? In binary, 5 is 0101 and 3 is 0011 (shown using four bits for simplicity). Applying AND, OR and XOR to the corresponding bits produces 1, 7 and 6.

Bitwise operators are useful in low-level operations, bit masks and certain performance-sensitive algorithms.

7. Shift Operators in Java

Shift operators move bits left or right.

Operator Meaning
<< Left shift
>> Signed right shift
>>> Unsigned right shift

Example:

int a = 8;

System.out.println(a << 1);
System.out.println(a >> 1);

Output:

16
4

For this positive number, shifting left by one doubles its value and shifting right by one halves it, discarding any remainder. More generally, shift operations manipulate the binary representation and require care with negative numbers and overflow.

8. Ternary Operator in Java

The ternary operator is a compact alternative to a simple if-else expression.

Syntax:

condition ? valueIfTrue : valueIfFalse

Example

int age = 20;

String result = (age >= 18) ? "Eligible" : "Not Eligible";

System.out.println(result);

Output:

Eligible

If the condition is true, the first value is selected. Otherwise, the second value is selected.

9. instanceof Operator in Java

The instanceof operator checks whether an object is an instance of a particular type.

String name = "Rahul";

System.out.println(name instanceof String);

Output:

true

This operator is often useful when working with objects and inheritance.

Operator Precedence in Java

Operator precedence determines which operators are evaluated first in an expression.

For example:

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

Output:

20

Multiplication occurs before addition.

Use parentheses to make an expression clearer:

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

Output:

30

Parentheses make the addition happen first. When an expression becomes complicated, parentheses improve readability and reduce mistakes.

Complete Practical Java Program

The following program combines arithmetic, comparison, logical and ternary operators.

public class Main {
    public static void main(String[] args) {
        int marks = 75;
        int passingMarks = 33;

        int bonusMarks = 5;
        int finalMarks = marks + bonusMarks;

        boolean passed = finalMarks >= passingMarks;
        boolean excellent = finalMarks >= 80;

        String result = passed ? "Pass" : "Fail";

        System.out.println("Original Marks: " + marks);
        System.out.println("Final Marks: " + finalMarks);
        System.out.println("Passed: " + passed);
        System.out.println("Excellent: " + excellent);
        System.out.println("Result: " + result);
    }
}

Output:

Original Marks: 75
Final Marks: 80
Passed: true
Excellent: true
Result: Pass

This example shows how operators can calculate values, evaluate conditions and select a result.

Common Mistakes Beginners Should Avoid

  1. Confusing = with ==: One assigns a value; the other compares values.

  2. Expecting decimal results from integer division: 5 / 2 produces 2, while 5.0 / 2 produces 2.5.

  3. Misunderstanding increment operators: a++ and ++a differ when used inside a larger expression.

  4. Using & instead of &&: For Boolean operands, && short-circuits; & evaluates both operands.

  5. Ignoring operator precedence: Use parentheses when the intended order might be unclear.

  6. Comparing String contents with ==: Use equals() for content comparison.

Frequently Asked Questions (FAQs)

1. What is an operator in Java?

An operator is a symbol that performs an operation on one or more operands, such as addition, comparison or assignment.

2. What is an expression in Java?

An expression is a combination of values, variables and operators that evaluates to a result.

3. What is the difference between = and ==?

= assigns a value to a variable, while == checks equality for primitive values or reference identity for objects.

4. What is the difference between && and ||?

&& requires both conditions to be true. || requires at least one condition to be true.

5. What is the ternary operator?

The ternary operator ?: chooses between two expressions based on a condition.

6. What is operator precedence?

Operator precedence defines which operators are evaluated first in an expression.

7. What does the modulus operator % do?

It returns the remainder after division. For example, 10 % 3 returns 1.

Practice Exercises

Try these exercises to strengthen your understanding:

  1. Write a program that adds, subtracts, multiplies and divides two integers.

  2. Check whether a number is greater than 100.

  3. Use && to check whether an age falls between 18 and 60, inclusive.

  4. Use the ternary operator to print whether a number is even or odd.

  5. Demonstrate the difference between a++ and ++a.

  6. Predict the result of 10 + 2 * 3.

  7. Use += to update a variable and print its new value.

Challenge: Create a simple marks calculator that calculates total marks, average marks and pass/fail status using Java operators.

Conclusion

Java operators help programmers perform calculations, compare values, evaluate logical conditions and assign results. Understanding these operators is essential before learning conditional statements and loops.

Practise the examples in your own Java environment and predict the output before running each program.