Operators in Java: Types, Precedence and Examples

Operators in Java are symbols that tell the compiler to perform a specific operation on one or more values called operands. Java defines seven operator types: arithmetic, relational, logical, bitwise, assignment, ternary and instanceof. They appear in every line of Java code, from simple addition to complex type checks, and understanding them is essential for writing correct, predictable programs.

  • Java has seven operator categories, each serving a distinct purpose in controlling data and program flow.
  • Operator precedence determines evaluation order, and ignoring it is one of the most common sources of bugs in beginner code.
  • The ternary operator (?:) is a compact alternative to an if-else block, useful for simple one-line decisions.
  • The instanceof operator checks an object’s type at runtime, which matters a lot in polymorphism and type-safe casting.
  • Strong operator knowledge is tested in every Java interview, from campus placements at TCS and Infosys to product companies like Flipkart.

What Operators Are

An operator in Java is a symbol that tells the compiler to perform a specific action on one or more operands. An operand is simply the value or variable the operator acts upon. In the expression a + b, the plus sign is the operator and a and b are the operands.

Java inherits much of its operator syntax from C and C++, which is why it feels familiar if you have already read about how to write, compile and run a C program. The rules are stricter in Java, though. Java does not let you overload operators the way C++ does, so the meaning of each symbol is fixed and consistent across every codebase.

According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, with around 30% of professional developers working with it regularly. The JetBrains State of Developer Ecosystem 2024 report found that Java is the primary language for 27% of backend developers worldwide, reinforcing its dominance in enterprise and product engineering. That sustained adoption is why understanding operators in Java is a fundamental job skill, not just an academic exercise.

Unary, Binary and Ternary by Operand Count

Java operators are also classified by how many operands they need. A unary operator works on one operand, for example ++count or !flag. A binary operator needs two, like a * b. The ternary operator needs three, which we cover in detail below.

Knowing this distinction helps you read complex expressions faster. When you see -x in code, you immediately know it is a unary negation, not subtraction, because there is no left-hand operand. That kind of quick parsing is what separates confident Java developers from those who guess their way through expressions.

Arithmetic and Relational Operators

Arithmetic Operators in Java

Arithmetic operators in Java handle standard mathematical operations. They work on numeric types: int, long, float, double and their wrappers. The full set is shown in the table below.

Operator Name Example Result (a=10, b=3)
+ Addition a + b 13
- Subtraction a - b 7
* Multiplication a * b 30
/ Division a / b 3 (integer division)
% Modulus a % b 1
++ Increment a++ 10 (post), 11 (pre)
-- Decrement a-- 10 (post), 9 (pre)

One real-world mistake beginners make constantly: dividing two integers in Java always gives an integer result. If you write 10 / 3, you get 3, not 3.33. You need at least one operand to be a double or cast explicitly, like (double) 10 / 3, to get the decimal result. This trips up students in their first Java lab at colleges like BITS Pilani and VIT alike.

Relational Operators in Java

Relational operators in Java, also called Java comparison operators, compare two values and always return a boolean: either true or false. They are the backbone of every if statement and loop condition you will ever write.

The six relational operators are: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to) and <= (less than or equal to). Using == to compare two String objects is one of the most common Java bugs. It checks reference equality, not content. Use .equals() for strings. This is the kind of subtle mistake that causes Java exceptions and common runtime errors that are frustrating to debug.

Logical and Bitwise Operators

Logical operators combine boolean expressions. The Java AND operator is &&, the Java OR operator is ||, and logical NOT is !. These are short-circuit operators, meaning Java stops evaluating as soon as the result is determined. If the left side of && is false, the right side never runs.

Bitwise operators work directly on binary representations of integers: &, |, ^ (XOR), ~ (complement) and shift operators <<, >>, >>>. They are used heavily in cryptography, network programming and low-level system code, which is why understanding them matters if you are heading toward CEH v13 AI online course and cybersecurity.

In India, Java is a core requirement in technical hiring at TCS, Wipro and Infosys, which collectively employ hundreds of thousands of Java developers. According to NASSCOM’s FY2024 IT sector report, Java-based backend roles account for a significant share of entry-level engineering positions across Indian IT services firms, making operator fluency a practical hiring prerequisite, not just a syllabus item.

Ternary, instanceof and Others

The Java Question Mark Operator (Ternary)

The ternary operator in Java uses the syntax condition ? valueIfTrue : valueIfFalse. It is the only operator in Java that takes three operands, which is why it is called ternary. A simple example: String result = (score >= 40) ? "Pass" : "Fail"; does the same job as a four-line if-else block.

Use it for simple, readable one-liners. Do not nest ternary operators inside each other. Nested ternaries become nearly impossible to read and maintain, and code review will flag them immediately in any professional team. Keep the condition short and the values obvious.

The instanceof Operator in Java

The instanceof operator checks whether an object is an instance of a specific class or interface. The syntax is object instanceof ClassName, and it returns true or false. It is most useful before a type cast to avoid a ClassCastException at runtime.

For example: if (animal instanceof Dog) { Dog d = (Dog) animal; d.bark(); }. Java 16 introduced pattern matching for instanceof, so you can now write if (animal instanceof Dog d) { d.bark(); } and skip the explicit cast entirely. This is part of the Oracle Java documentation‘s ongoing effort to make type-safe code less verbose.

Assignment Operators

The basic assignment operator is =. Java also supports compound assignment operators that combine an arithmetic or bitwise operation with assignment: +=, -=, *=, /=, %=, &=, |= and ^=. Writing x += 5 is exactly equivalent to x = x + 5, just shorter.

One thing worth knowing: compound assignment operators include an implicit cast. If x is a byte, writing x += 1 compiles fine, but x = x + 1 throws a compile error because the right-hand side is promoted to int. It is a small detail that shows up in Java certification exams and technical interviews.

Operator Precedence

Operator precedence in Java, sometimes called Java operator priority, determines which operation runs first when an expression has multiple operators. Java follows a strict hierarchy, much like BODMAS in mathematics. Higher-precedence operators bind more tightly to their operands.

The TIOBE Index (April 2025) ranks Java consistently in the top three programming languages by popularity. Every Java developer at every level needs to understand precedence because a misread expression can silently produce wrong results without throwing any error at all. That is the dangerous kind of bug.

Precedence Table (High to Low)

Level Operators Associativity
1 (Highest) ++ -- (postfix), (), [], . Left to right
2 ++ -- (prefix), + - (unary), ! ~ Right to left
3 * / % Left to right
4 + - Left to right
5 << >> >>> Left to right
6 < > <= >= instanceof Left to right
7 == != Left to right
8 & Left to right
9 ^ Left to right
10 | Left to right
11 && Left to right
12 || Left to right
13 ?: Right to left
14 (Lowest) = += -= etc. Right to left

Precedence in Practice

Consider int result = 2 + 3 * 4;. Because * has higher precedence than +, multiplication runs first: 3 * 4 = 12, then 2 + 12 = 14. If you actually want addition first, use parentheses: (2 + 3) * 4 = 20. Parentheses always win, sitting at the very top of the precedence table.

A practical tip from real code reviews: when you are unsure about precedence in a complex expression, add parentheses explicitly. It makes your intent clear to other developers and removes any chance of ambiguity. Readable code is maintainable code, and that matters more than saving a pair of brackets.

If you are building your broader programming foundation, check out this guide on software development tools to learn first. Operators are only one piece of the puzzle; your tooling matters too.

Students pursuing a BSc Computer Science subjects and scope or an MCA will encounter operator-based questions in data structures, algorithms and object-oriented programming papers. Java is also a core subject in the GATE CS syllabus, making operator precedence a recurring exam topic across Indian universities. If you are thinking about career outcomes after your degree, the guide on jobs and salary after MCA is worth reading alongside your technical prep.

Whether you are aiming to become a full stack developer vs software developer, Java fundamentals including operators will come up in technical rounds. Practising operator programs in Java, writing small programs that test precedence and short-circuit logic, is the fastest way to build confidence.

Frequently Asked Questions

What are the operators in Java?

Operators in Java are symbols that perform operations on one or more operands. Java defines seven main types: arithmetic, relational, logical, bitwise, assignment, ternary and instanceof. Each category serves a specific purpose, from basic math to type checking, and every Java program uses them constantly to control data and logic flow.

What is operator precedence in Java?

Operator precedence in Java is the set of rules that determines which operator is evaluated first in an expression containing multiple operators. For example, * is evaluated before +, just like in standard mathematics. You can always override the default order by wrapping expressions in parentheses, which have the highest precedence of all.

What is the question mark operator in Java?

The question mark operator in Java is the ternary operator, written as condition ? valueIfTrue : valueIfFalse. It is a shorthand for a simple if-else statement and returns one of two values based on whether the condition evaluates to true or false. It is best used for short, clear expressions where nesting it would reduce readability.

What does instanceof do in Java?

The instanceof operator checks whether a given object is an instance of a specified class or interface, returning true or false. It is commonly used before type casting to prevent a ClassCastException. From Java 16 onward, pattern matching with instanceof allows you to declare the cast variable inline, making the code shorter and safer.

What are relational operators in Java?

Relational operators in Java compare two values and return a boolean result. The six operators are ==, !=, >, <, >= and <=. They are used in every conditional statement and loop. A critical point: never use == to compare String objects for content equality; use the .equals() method instead.

You now have a complete picture of operators in Java, from definitions and syntax to precedence rules and real usage traps. The next step is writing code that tests these concepts deliberately, not just reading about them.

  1. Write 10 small Java programs this week, each focused on one operator category. Include at least two that specifically test precedence by predicting the output before running the code.
  2. Review the Java exceptions and common runtime errors guide next, since many runtime errors connect directly to incorrect operator use, especially with null references and type mismatches.
  3. Explore 3.0 University online certification courses if you want to take your Java skills into applied domains. Strong operator and logic fundamentals are the entry point to secure coding, ethical hacking and AI development. The CEH v13 AI online course at 3.0 University is a natural next step for anyone who wants to move from writing Java code to understanding how attackers and defenders use it.

Last updated: June 2025. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles