Java Operators

What are Operators in Java?

May 11th, 2026
5706
07:00 Minutes

Java operators are special symbols that are used to perform operations on variables and values in a Java program. They help you calculate results, compare data, apply conditions, and control how your program executes. In simple terms, operators tell Java what action to perform.

Operators in Java are extremely important because every program depends on them. Operators are involved in many things like in adding numbers, checking if a condition is true, updating a variable inside a loop, making decisions using if statements, etc. You cannot perform arithmetic calculations, evaluate logical expressions, or control program flow without the use of operators. They are like the foundation of expressions, conditions, and decision-making in Java programming.

Understanding the types of operators in Java with examples is one of the first major steps for beginners and students. So, let’s begin by understanding what are operators in Java and types of operators in Java.

Also Read: Java Tutorial for Beginners

What are operators in Java?

In Java programming, operators are special symbols that perform operations on variables and values (called as operands). They are used to create expressions that calculate results, compare data, assign values, and control decision-making in a program.

Whenever you write an expression in Java, you are using one or more operators. Operators make all the logic possible, from adding numbers, checking conditions to updating a variable inside a loop.

Basic Example of an Operator in Java

int a = 10;
int b = 5;
int result = a + b;

In this example:

  • a and b are operands
  • + is the arithmetic operator
  • a + b is the expression
  • result stores the computed value
  • + operator will perform addition and return the value 15

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Operators and Expressions in Java

An expression in Java is a combination of variables, values, and operators that produces a result. For instance, you can see the expression given below:

int total = (5 + 3) * 2;

This expression shows how operators follow specific execution rules such as precedence and associativity. Let’s understand its step-by-step execution:

  1. Java first evaluates parentheses → 5 + 3 = 8
  2. Then multiplication → 8 * 2 = 16
  3. It will return the OUTPUT → 16

Java Operator Precedence (Highest to Lowest)

Precedence Level Operators Description Associativity
1 (Highest) ++ -- + - ! ~ Unary operators Right to Left
2 * / % Multiplication, Division, Modulus Left to Right
3 + - Addition, Subtraction Left to Right
4 << >> >>> Shift operators Left to Right
5 < <= > >= Relational operators Left to Right
6 == != Equality operators Left to Right
7 & Bitwise AND Left to Right
8 ^ Bitwise XOR Left to Right
9 | Bitwise OR Left to Right
10 && Logical AND Left to Right
11 || Logical OR Left to Right
12 ? : Ternary operator Right to Left
13 (Lowest) = += -= *= /= %= Assignment operators Right to Left

Also Explore: How to Install Java for Installation

Types of Operators in Java

In Java programming, operators are divided into different categories based on the type of operation they perform. Understanding the types of operators in Java helps beginners write clearer code and choose the correct operator for a specific task.

Java provides the following main types of operators:

  • Arithmetic Operators

Arithmetic operators in Java are symbols used to perform mathematical calculations on numeric values such as integers and decimal numbers. They help a program to add, subtract, multiply, divide or calculate the remainder between two numbers. These operators work on numeric data types like int, double, float, long and short. Then, they return a computed result based on the operation performed.

In simple terms, arithmetic operators are what make calculations possible in a Java program. The Arithmetic Operators are as follows:

Operator Description Example Output
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 3 3
% Modulus (remainder) 10 % 3 1

Java Arithmetic Operator Example:

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

        int a = 20;
        int b = 6;

        // Addition
        int addition = a + b;

        // Subtraction
        int subtraction = a - b;

        // Multiplication
        int multiplication = a * b;

        // Division
        int division = a / b;

        // Modulus (Remainder)
        int modulus = a % b;

        System.out.println("Addition: " + addition);
        System.out.println("Subtraction: " + subtraction);
        System.out.println("Multiplication: " + multiplication);
        System.out.println("Division: " + division);
        System.out.println("Modulus (Remainder): " + modulus);
    }
}

Output:

Addition: 26
Subtraction: 14
Multiplication: 120
Division: 3
Modulus (Remainder): 2

java arithmetic operator example

  • Relational Operators

Relational operators in Java are used to compare two values, variables, or expressions. Their main purpose is to determine the relationship between two operands, such as whether they are equal, different, greater than, or less than each other. Unlike arithmetic operators, relational operators do not perform calculations. Instead, they evaluate a condition and always return a Boolean result. This means the outcome will either be true or false.

For instance, if you compare two numbers and the condition is satisfied. Then, the result will be true; otherwise, it will be false. This Boolean result helps Java programs to make decisions. The relational operators are as follows:

Operator Description Example Output 
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater than or equal to 5 >= 5 true
<= Less than or equal to 3 <= 5 true

Java Relational Operators Example:

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

        int a = 15;
        int b = 10;

        System.out.println("Equal to (a == b): " + (a == b));
        System.out.println("Not equal to (a != b): " + (a != b));
        System.out.println("Greater than (a > b): " + (a > b));
        System.out.println("Less than (a < b): " + (a < b));
        System.out.println("Greater than or equal to (a >= b): " + (a >= b));
        System.out.println("Less than or equal to (a <= b): " + (a <= b));
    }
}

Output:

Equal to (a == b): false
Not equal to (a != b): true
Greater than (a > b): true
Less than (a < b): false
Greater than or equal to (a >= b): true
Less than or equal to (a <= b): false

java relational operator example

Read Also: Final Keyword in Java

  • instanceof Operator

The instanceof operator in Java is used to check whether an object belongs to a specific class, subclass, or interface. It helps determine the type of an object at runtime. This operator returns a Boolean value, which means the result will either be true or false.

The instanceof operator is especially useful in object-oriented programming when working with inheritance, polymorphism, or when handling objects of different types. It ensures type safety before performing type casting and helps prevent ClassCastException errors.

Operator Description Example Output
instanceof Checks whether an object belongs to a specific class or interface "Java" instanceof String true

Java instanceof Operator Example:

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

        String language = "Java";

        if (language instanceof String) {
            System.out.println("Yes, it is a String");
        }

        Object obj = 100;

        if (obj instanceof Integer) {
            System.out.println("Object is an Integer");
        }
    }
}

Output:

Yes, it is a String
Object is an Integer

Modern Update (Java 16 and Later): Starting from Java 16, pattern matching was introduced with instanceof. This allows type casting directly inside the condition, making the code cleaner and more readable.

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

        Object value = "Java Operators";

        if (value instanceof String str) {
            System.out.println(str.toUpperCase());
        }
    }
}

This modern feature improves readability and reduces unnecessary type casting in Java programs.

Read Also: Java 21 Features

  • Logical Operators

Java logical operators are used to modify or combine Boolean expressions. They are mainly used when a program needs to evaluate more than one condition at the same time. It is not like arithmetic operators that work with numbers. Logical operators work with boolean values. This means they deal only with true or false.

In simple terms, logical operators help a Java program make smarter decisions by checking multiple conditions together. The Logical Operators are as follows:

Operator Description Rule
&& Logical AND True if both conditions are true
! Logical NOT Reverses the boolean value
|| Logical OR True if at least one condition is true

Java Logical AND and Logical NOT example:

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

        int age = 22;
        boolean hasID = true;

        // Logical AND
        boolean canEnterClub = (age >= 21) && hasID;

        // Logical NOT
        boolean isMinor = !(age >= 18);

        // Logical NOT
        boolean canEnter = (age >= 21) || hasID;

        System.out.println("Logical AND (age >= 21 && hasID): " + canEnterClub);
        System.out.println("Logical NOT !(age >= 18): " + isMinor);
        System.out.println("Logical OR (age >= 21 || hasID): " + canEnter);
    }
}

Output:

Logical AND (age >= 21 && hasID): true
Logical NOT !(age >= 18): false

java logical & and logical not example

  • Assignment Operators

Assignment operators in Java are used to assign values to variables and in updating their existing values. They play a fundamental role in programming because they help data to be stored, modified, and reused throughout a program. Variables would not be able to hold or change values, and meaningful computation would not be possible without assignment operators

The most basic assignment operator is the equals sign (=). It assigns the value on the right-hand side to the variable on the left-hand side. The assignment operators are as follows:

Operator Description Equivalent To
= Assign value a = 5
+= Add and assign a = a + 5
-= Subtract and assign a = a - 5
*= Multiply and assign a = a * 5
/= Divide and assign a = a / 5
%= Modulus and assign a = a % 5

Java Assignment Operator example:

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

        int number;

        // Assign value
        number = 20;
        System.out.println("Assign value (=): " + number);

        // Add and assign
        number += 5;
        System.out.println("Add and assign (+=): " + number);

        // Subtract and assign
        number -= 3;
        System.out.println("Subtract and assign (-=): " + number);

        // Multiply and assign
        number *= 2;
        System.out.println("Multiply and assign (*=): " + number);

        // Divide and assign
        number /= 4;
        System.out.println("Divide and assign (/=): " + number);

        // Modulus and assign
        number %= 3;
        System.out.println("Modulus and assign (%=): " + number);
    }
}

Output:

Assign value (=): 20
Add and assign (+=): 25
Subtract and assign (-=): 22
Multiply and assign (*=): 44
Divide and assign (/=): 11
Modulus and assign (%=): 2

java assignment operator example

  • Unary Operators

Unary operators in Java are operators that work on only one operand. This is not like arithmetic or relational operators that require two values to perform an operation. Unary operators act on a single variable or value and modify or evaluate it directly.

The word “unary” means “one”. It clearly explains their behavior. These operators are commonly used to increment a value, decrement a value, change the sign of a number or reverse a boolean condition. Even though they operate on just one operand, they play a very important role in controlling how values change during program execution. The Unary Operators are as follows:

Operator Description Example
++ Increment by 1 a++
-- Decrement by 1 a--
+ Unary plus +a
- Unary minus -a
! Logical NOT !true

Java Unary Operator Example:

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

        int number = 5;
        boolean status = true;

        // Increment by 1
        number++;
        System.out.println("Increment (number++): " + number);

        // Decrement by 1
        number--;
        System.out.println("Decrement (number--): " + number);

        // Unary plus
        int positiveValue = +number;
        System.out.println("Unary plus (+number): " + positiveValue);

        // Unary minus
        int negativeValue = -number;
        System.out.println("Unary minus (-number): " + negativeValue);

        // Logical NOT
        boolean reversedStatus = !status;
        System.out.println("Logical NOT (!status): " + reversedStatus);
    }
}

Output:

Increment (number++): 6
Decrement (number--): 5
Unary plus (+number): 5
Unary minus (-number): -5
Logical NOT (!status): false

java unary operator example

  • Bitwise Operators

Bitwise operators in Java perform operations directly on the binary representation of numbers. They do not work with decimal values like arithmetic operators. Bitwise operators manipulate individual bits like 0s and 1s inside an integer. Since every number in a computer is stored in binary form, bitwise operators allow programmers to work at a lower level of data processing.

In Java, bitwise operators are mainly used with integer data types such as int, long, short, and byte. When a bitwise operation is performed, Java converts the numbers into binary form, applies the operation bit by bit and then converts the result back into a decimal number. The Bitwise operators are follows:

Operator Description Example Output
& Bitwise AND 5 & 3 1
` ` Bitwise OR `5
^ XOR 5 ^ 3 6
~ Complement ~5 -6
<< Left Shift 5 << 1 10
>> Right Shift 5 >> 1 2

Java Bitwise Operators Example:

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

        int a = 5;   // Binary: 0101
        int b = 3;   // Binary: 0011

        // Bitwise AND
        int andResult = a & b;

        // Bitwise OR
        int orResult = a | b;

        // Bitwise XOR
        int xorResult = a ^ b;

        // Bitwise Complement
        int complementResult = ~a;

        // Left Shift
        int leftShiftResult = a << 1;

        // Right Shift
        int rightShiftResult = a >> 1;

        System.out.println("Bitwise AND (a & b): " + andResult);
        System.out.println("Bitwise OR (a | b): " + orResult);
        System.out.println("Bitwise XOR (a ^ b): " + xorResult);
        System.out.println("Bitwise Complement (~a): " + complementResult);
        System.out.println("Left Shift (a << 1): " + leftShiftResult);
        System.out.println("Right Shift (a >> 1): " + rightShiftResult);
    }
}

Output:

Bitwise AND (a & b): 1
Bitwise OR (a | b): 7
Bitwise XOR (a ^ b): 6
Bitwise Complement (~a): -6
Left Shift (a << 1): 10
Right Shift (a >> 1): 2

java bitwise operators example

  • Ternary Operator

Java ternary operator is a conditional operator. It is used to evaluate a condition and return one of two values based on whether that condition is true or false. It is called ternary because it works with three parts: a condition, a value returned if the condition is true, and a value returned if the condition is false. It is written using the symbols : and ?. It is considered a shorter and a more compact version of a simple if-else statement.

Java Ternary Operator Example:

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

        int number = 15;

        // Using ternary operator
        String result = (number % 2 == 0) ? "Even Number" : "Odd Number";

        System.out.println("Number: " + number);
        System.out.println("Result: " + result);
    }
}

Output:

Number: 15
Result: Odd Number

java ternary operator example

Related Article: Keywords in Java

Common Mistakes to Avoid

Operators in Java look simple but sometimes beginners make mistakes that lead to wrong output or logical errors. Understanding these common issues will help you avoid bugs and write better programs.

  • Confusing assignment (=) with equality (==): This is one of the most common mistakes of beginners. The = operator assigns a value to a variable, and == operator compares two values. If you write (a = 5) in place of (a == 5). It can cause logical errors or compilation issues.
  • Forgetting how integer division works: When both operands are integers, then Java removes the decimal part. For instance, 7 / 2 gives 3 as output, not 3.5. Many students expect a decimal result without using double.
  • Ignoring operator precedence rules: Java follows a specific order of execution. For instance, multiplication happens before addition. Writing 10 + 5 * 2 will always give 20, not 30. This is why understanding precedence is important; otherwise, your calculations may produce unexpected results.
  • Overusing complex expressions in one line: Combining too many operators in a single statement makes the code hard to read and debug. This often leads to logical mistakes.
  • Misunderstanding logical operators (&& and ||): If you use || (OR) when you actually need && (AND). Then, this can completely change your program’s behavior. That is why always verify whether both conditions must be true or only one.
  • Confusion between pre-increment and post-increment (++a vs a++): The timing of value change matters. This small difference can produce different outputs in loops and expressions.
  • Using bitwise operators instead of logical operators by mistake: When you write & instead of && or | in place of ||. It can change how conditions are evaluated and may remove short-circuit behavior.

Also Explore: Classes and Objects in Java

Tips and Best Practices

If you want to master operators in Java, you should focus not only on syntax but also on writing clean, logical, and efficient code.

  • Use parentheses to improve clarity: Using parentheses makes expressions easier to understand and reduces confusion about operator precedence.
  • Write readable code instead of clever code: Clear and simple expressions are better than confusing ones. Code should be easy to understand for you and others.
  • Break complex logic into smaller steps: Instead of writing long expressions with multiple operators, divide them into smaller variables. This improves debugging and maintainability.
  • Understand operator precedence and associativity: If you know which operator executes first, it will help you predict output correctly.
  • Use compound assignment operators properly: Using operators like += and *= makes code shorter and more efficient. It is especially useful when it is inside loops.
  • Be careful when mixing data types: When you combine int and double, it can change the result type. Hence, you should always know what type your expression will return.
  • Test logical conditions carefully: When relational and logical operators are used together, you should always verify your conditions step by step to ensure that your program is in the correct flow.
  • Practice with real examples and output checking: The best way to master Java operators is by writing small programs and analyzing how each operator behaves.

Wrap-Up

Java operators are the foundation of every Java program. They allow you to perform calculations, compare values, apply logical conditions, and control how your code runs. Operators are used in almost every line of real-world Java applications from simple arithmetic operations to complex decision-making.

Understanding operators in Java will help you in building strong programming fundamentals. Once you master arithmetic, relational, logical, assignment, unary, bitwise, and ternary operators, then writing expressions and conditions will become much easier for you.

For that you need to practice regularly, test your output and focus on clarity. A strong understanding of Java operators will make advanced topics like loops, arrays, and object-oriented programming much easier to learn for you.

Explore Our Trending Articles-

Want to Learn Everything About Java Programming?

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

FAQs

Q1. How many types of operators are there in Java?

There are seven main types of operators in Java: arithmetic, relational, logical, assignment, unary, bitwise, and ternary operators. Each type is used for a specific purpose in expressions and decision-making.

Q2. Why are Java Operators important for Beginners?

Java operators are important because they form the foundation of programming logic. Java operators are used in every calculation, comparison, and condition. Understanding them early helps beginners in writing correct code and in avoiding common mistakes.

Q3. What is the difference between = and == in Java?

The = operator is used to assign a value to a variable, while == is used to compare two values. The assignment operator stores data and equality checks whether two values are the same.

Q4. What is operator precedence in Java?

Operator precedence in Java determines the order in which operators are evaluated in an expression. Operators with higher precedence are executed first, unless parentheses are used to change the order.

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled content writer with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.