Types of Operators in Java: A Complete Guide for Beginners
By Sriram
Updated on Jun 05, 2026 | 6 min read | 1.43K+ views
Share:
Looks like you're browsing from the
United StatesSome programs may not be available in your location
Some programs may not be available in your location
Switch to upGrad USAll courses
Certifications
More
By Sriram
Updated on Jun 05, 2026 | 6 min read | 1.43K+ views
Share:
Table of Contents
Java operators are the building blocks of every expression you write. Whether you're comparing values, doing math, or controlling logic, operators make it happen. Without understanding them, writing even basic Java code feels like guessing.
This blog covers all the types of operators in Java with clear examples, practical usage notes, and tables where they actually help. By the end, you'll know exactly which operator to use and when.
Explore upGrad's Data Science, AI, and Machine Learning programs to strengthen your foundation in Java fundamentals, including operators, variables, control flow, and object-oriented programming concepts.
Operators are one of the most fundamental building blocks in Java. They help you perform calculations, compare values, evaluate conditions, and manipulate data within a program. Without operators, even simple tasks like adding two numbers or checking whether a user is eligible to vote would require much more complex code.
An operator is a symbol that tells the compiler to perform a specific operation on one or more values. Those values are called operands.
Here's a simple example:
int result = 10 + 5;
The + is the operator. 10 and 5 are the operands. result gets 15.
Java has over 40 operators across different categories, and some of them behave in ways that catch even experienced developers off guard.
There are seven main types of operators in Java:
Each category serves a different purpose. Mixing them up or misreading their precedence causes bugs that are frustratingly hard to trace.
Must read: Data Types in Java: Primitive & Non-Primitive Data Types
Arithmetic operators handle basic math. You've been using these since you wrote your first program.
Operator |
Operation |
Example |
Result |
| + | Addition | 8 + 3 | 11 |
| - | Subtraction | 8 - 3 | 5 |
| * | Multiplication | 8 * 3 | 24 |
| / | Division | 8 / 3 | 2 |
| % | Modulus | 8 % 3 | 2 |
The one that trips people up is division. In Java, when you divide two integers, the result is also an integer. So 8 / 3 gives 2, not 2.67. That decimal gets silently dropped.
Want the decimal? Use a double:
double result = 8.0 / 3; // gives 2.6666...
The modulus operator % returns the remainder. It's not just for math class. It's widely used to check if a number is even or odd:
if (num % 2 == 0) {
System.out.println("Even");
}
The + operator does double duty in Java. With numbers, it adds. With strings, it concatenates.
String s = "Hello" + " World"; // "Hello World"
This causes unexpected behavior when mixing types. "Age: " + 5 + 3 gives "Age: 53", not "Age: 8". Order matters.
Do read: Types of Variables in Java: The Building Blocks of Clean Code
These two categories are usually explained separately but they work together constantly in real code.
Relational operators compare two values. They always return a boolean: true or false.
Operator |
Meaning |
Example |
Result |
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| > | Greater than | 7 > 4 | true |
| < | Less than | 3 < 1 | false |
| >= | Greater than or equal | 5 >= 5 | true |
| <= | Less than or equal | 3 <= 4 | true |
One mistake beginners make consistently: using = instead of ==. A single = is assignment. == is comparison. Java won't always catch this as an error, which makes the bug worse.
Logical operators combine multiple boolean expressions.
Operator |
Meaning |
Example |
| && | AND | a > 0 && b > 0 |
| || | OR | a > 0 || b > 0 |
| ! | NOT | !(a > 0) |
&& only returns true if both conditions are true. || returns true if at least one is. ! flips the result.
Java uses short-circuit evaluation with && and ||. If the first condition in an && expression is false, Java doesn't check the second. That's actually useful for avoiding null pointer errors:
if (str != null && str.length() > 0) {
// safe to use str
}
Also read: Control Statements in Java: Types, Flowcharts, and Code Examples
The basic assignment operator is =. But Java has compound assignment operators that combine an operation with assignment.
Operator |
Equivalent To |
Example |
| = | Direct assignment | x = 5 |
| += | x = x + value | x += 3 |
| -= | x = x - value | x -= 3 |
| *= | x = x * value | x *= 2 |
| /= | x = x / value | x /= 2 |
| %= | x = x % value | x %= 4 |
These aren't just shortcuts. They make code easier to read when you're updating a variable repeatedly.
Unary operators work on a single operand.
Operator |
What It Does |
Example |
| + | Indicates positive value | +5 |
| - | Negates a value | -5 |
| ++ | Increments by 1 | x++ or ++x |
| -- | Decrements by 1 | x-- or --x |
| ! | Logical NOT | !true |
The ++ and -- operators have a subtle difference depending on placement.
x++ is post-increment: uses the current value first, then increments. ++x is pre-increment: increments first, then uses the value.
int x = 5;
System.out.println(x++); // prints 5, then x becomes 6
System.out.println(++x); // x becomes 7, then prints 7
Don't underestimate this. It causes real bugs in loops and conditional logic.
Do read: Array in Java: Types, Operations, Pros & Cons
Bitwise operators work directly on binary representations of integers. You won't use them in every program, but in performance-sensitive code, cryptography, or low-level system work, they're essential.
Operator |
Operation |
Description |
| & | AND | Sets bit to 1 if both bits are 1 |
| | | OR | Sets bit to 1 if either bit is 1 |
| ^ | XOR | Sets bit to 1 if bits differ |
| ~ | Complement | Flips all bits |
| << | Left shift | Shifts bits left |
| >> | Right shift | Shifts bits right |
A left shift by 1 is equivalent to multiplying by 2. A right shift by 1 is equivalent to dividing by 2. That's why these show up in performance-optimized algorithms.
This is Java's only operator that takes three operands. It's a compact way to write an if-else expression.
Syntax: condition ? value_if_true : value_if_false
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
It reads well for simple conditions. But don't nest ternary operators. Nested ternaries are hard to read and harder to debug.
Must read: Identifiers in Java: Key Concepts, Syntax, Examples, and Best Practices to Know in 2025
When an expression has multiple operators, Java doesn't just go left to right. It follows a precedence order.
Precedence |
Operators |
| Highest | ++, -- (unary), ! |
| *, /, % | |
| +, - | |
| <, >, <=, >= | |
| ==, != | |
| & | |
| ^ | |
| | | |
| && | |
| || | |
| Lowest | =, +=, -=, etc. |
Take this expression: 5 + 3 * 2
Is it 16 or 11? It's 11, because * runs before +.
When you're not sure about precedence, use parentheses. They make intent explicit and remove ambiguity:
int result = (5 + 3) * 2; // clearly 16
This matters most in complex conditions and math-heavy logic. A missing pair of parentheses can produce wrong results silently.
Do read: What is Type Conversion in Java? [With Examples]
Operators in Java aren't just syntax. They define how your logic runs, how values get compared, and how your program makes decisions. The different types of operators in Java each have rules, edge cases, and real implications.
Start with arithmetic and relational operators. Get comfortable with logical operators. Then work your way to bitwise and the ternary operator once you're writing more complex programs. And always use parentheses when you're mixing operator types. It's the simplest way to avoid the kind of bug that takes an hour to find.
Ready to start your journey? Book a free consultation with upGrad today to find the best path for your career.
Most Java applications rely heavily on arithmetic, relational, logical, and assignment operators. These operators appear in calculations, form validations, business rules, loops, and conditional statements.
Bitwise and shift operators are less common in day-to-day development but become important in areas like cryptography, networking, game development, and systems programming.
When both operands are integers, Java performs integer division. Any decimal portion of the result is discarded rather than rounded.
For example, 7 / 2 returns 3, not 3.5. To get a decimal result, at least one operand must be a floating-point value such as 7.0 / 2.
The && operator evaluates boolean expressions and uses short-circuit evaluation. If the first condition is false, Java skips the second condition.
The & operator works at the bit level for integers and can also evaluate both boolean expressions regardless of the first result. This distinction often appears in technical interviews.
Historically, developers used shift operators for performance optimization because shifting bits is mathematically similar to multiplying or dividing by powers of two.
Modern JVMs already optimize arithmetic operations efficiently. In most applications, readability matters more than replacing multiplication or division with bit shifting.
Many beginners confuse = with ==, misunderstand integer division, or misuse logical operators such as && and ||.
Another common issue is ignoring operator precedence. A complex expression may compile successfully but produce unexpected results because operators execute in a specific order.
Unlike C++, Java does not support user-defined operator overloading. Developers cannot create custom behavior for operators such as + or * for their own classes.
The only exception users notice is string concatenation. Java internally supports the + operator for strings, but developers cannot extend this behavior to custom objects.
The ternary operator tests whether a candidate understands conditional expressions and concise coding practices. It can replace simple if-else blocks in a single line.
Interviewers also use it to evaluate code readability. While useful, overly complex ternary expressions often make code harder to maintain.
Relational and logical operators form the foundation of conditional logic. Statements such as if, while, for, and switch depend on operator evaluation.
Without these operators, programs couldn't compare values, validate inputs, or execute different actions based on changing conditions.
Once you're comfortable with the different types of operators in Java, focus on conditional statements, loops, arrays, and methods.
These concepts depend heavily on operators. A solid understanding of operator behavior makes learning control flow and problem-solving significantly easier.
Coding tests frequently include operator-related questions because they reveal how well a candidate understands Java fundamentals.
Questions often involve precedence rules, increment operators, bitwise operations, boolean logic, and output prediction. Small operator mistakes can change the final answer completel
A practical approach is grouping operators by purpose rather than memorizing symbols. Arithmetic operators perform calculations, relational operators compare values, and logical operators combine conditions.
Then practice writing small programs using each category. Real examples help concepts stick much better than memorizing operator tables alone.
420 articles published
Sriram K is a Senior SEO Executive with a B.Tech in Information Technology from Dr. M.G.R. Educational and Research Institute, Chennai. With over a decade of experience in digital marketing, he specia...
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled