Operators in Java
An operator is a symbol that performs an operation on one or more values. The values an operator works on are called operands.
int sum = 10 + 5; // + is the operator, 10 and 5 are the operandsJava groups its operators into a few families, and this page walks through each one.
Types of Operators
| Category | Operators |
|---|---|
| Arithmetic | + - * / % |
| Unary | + - ++ -- ! |
| Assignment | = += -= *= /= %= |
| Relational | == != > < >= <= |
| Logical | && || ! |
| Bitwise | & | ^ ~ |
| Shift | << >> >>> |
| Ternary | ? : |
| Type comparison | instanceof |
Arithmetic Operators
These perform basic mathematics.
public class Main { public static void main(String[] args) { int a = 17; int b = 5;
System.out.println(a + b); // 22 System.out.println(a - b); // 12 System.out.println(a * b); // 85 System.out.println(a / b); // 3 System.out.println(a % b); // 2 }}Two points worth remembering:
- Division between two
intvalues gives anint. The decimal part is dropped, not rounded, so17 / 5is3. - The modulus operator
%returns the remainder and works on floating point numbers too.
System.out.println(17 / 5); // 3System.out.println(17 / 5.0); // 3.4System.out.println((double) 17 / 5); // 3.4System.out.println(-17 % 5); // -2 (sign follows the left operand)Dividing an integer by zero throws ArithmeticException, but dividing a double by zero gives Infinity or NaN.
System.out.println(10.0 / 0); // InfinitySystem.out.println(0.0 / 0); // NaN// System.out.println(10 / 0); // ArithmeticExceptionThe + Operator with Strings
When one operand is a String, + joins text instead of adding numbers.
System.out.println("Total: " + 10 + 5); // Total: 105System.out.println("Total: " + (10 + 5)); // Total: 15Java evaluates left to right, so in the first line "Total: " + 10 already produced a string.
Unary Operators
A unary operator works on a single operand.
int number = 10;
System.out.println(-number); // -10System.out.println(++number); // 11System.out.println(--number); // 10
boolean isReady = false;System.out.println(!isReady); // truePrefix vs Postfix
++ and -- behave differently depending on where they are placed.
int a = 5;System.out.println(a++); // prints 5, then a becomes 6System.out.println(a); // 6
int b = 5;System.out.println(++b); // b becomes 6, then prints 6| Form | Meaning |
|---|---|
a++ | Use the current value, then increase it |
++a | Increase first, then use the new value |
The difference only matters when the value is used in the same expression.
Assignment Operators
= stores a value in a variable. The compound forms combine an operation with assignment.
int total = 10;
total += 5; // total = total + 5 -> 15total -= 3; // 12total *= 2; // 24total /= 4; // 6total %= 4; // 2Compound operators perform an implicit cast, which plain arithmetic does not.
byte value = 10;value += 5; // fine, cast happens automatically// value = value + 5; // compile error, int cannot be assigned to byteRelational Operators
These compare two values and always produce a boolean.
int a = 10;int b = 20;
System.out.println(a == b); // falseSystem.out.println(a != b); // trueSystem.out.println(a > b); // falseSystem.out.println(a < b); // trueSystem.out.println(a >= 10); // trueSystem.out.println(b <= 15); // false== with Objects
For objects, == compares references, not contents. Use equals() to compare values.
String first = new String("java");String second = new String("java");
System.out.println(first == second); // false, different objectsSystem.out.println(first.equals(second)); // true, same textLogical Operators
Logical operators combine boolean expressions.
| Operator | Name | Result |
|---|---|---|
&& | AND | true only if both sides are true |
|| | OR | true if at least one side is true |
! | NOT | Reverses the value |
int age = 22;boolean hasLicence = true;
System.out.println(age >= 18 && hasLicence); // trueSystem.out.println(age < 18 || hasLicence); // trueSystem.out.println(!hasLicence); // falseShort-Circuit Evaluation
&& skips the right side when the left side is already false, and || skips it when the left side is already true. This is often used to guard against errors.
String name = null;
if (name != null && name.length() > 3) { System.out.println("Valid name");}Here name.length() is never called, so there is no NullPointerException.
The single character forms & and | also work on booleans, but they always evaluate both sides.
Bitwise Operators
Bitwise operators work on the individual bits of integer types.
| Operator | Name | Description |
|---|---|---|
& | AND | Bit is 1 if both bits are 1 |
| | OR | Bit is 1 if either bit is 1 |
^ | XOR | Bit is 1 if the bits are different |
~ | Complement | Flips every bit |
int a = 5; // 0101int b = 3; // 0011
System.out.println(a & b); // 1 -> 0001System.out.println(a | b); // 7 -> 0111System.out.println(a ^ b); // 6 -> 0110System.out.println(~a); // -6A common use is checking whether a number is even.
int number = 42;System.out.println((number & 1) == 0); // true means evenShift Operators
Shift operators move bits left or right.
| Operator | Name | Description |
|---|---|---|
<< | Left shift | Shifts bits left, fills with 0 |
>> | Signed right shift | Shifts bits right, keeps the sign |
>>> | Unsigned right shift | Shifts bits right, fills with 0 |
int number = 8; // 00001000
System.out.println(number << 1); // 16, same as number * 2System.out.println(number >> 1); // 4, same as number / 2
int negative = -8;System.out.println(negative >> 1); // -4System.out.println(negative >>> 1); // 2147483644Left shifting by n multiplies by 2ⁿ, and signed right shifting divides by 2ⁿ.
Ternary Operator
The ternary operator is a short form of if-else that returns a value.
condition ? valueIfTrue : valueIfFalseint marks = 72;String result = marks >= 40 ? "Pass" : "Fail";System.out.println(result); // PassIt is best for simple choices. Nested ternaries become hard to read, so prefer if-else there.
int a = 15;int b = 25;int max = a > b ? a : b;System.out.println(max); // 25The instanceof Operator
instanceof checks whether an object is of a given type and returns a boolean.
Object value = "Hello Java";
if (value instanceof String) { String text = (String) value; System.out.println(text.length());}Since Java 16, pattern matching lets you declare the variable in the same statement.
Object value = "Hello Java";
if (value instanceof String text) { System.out.println(text.length()); // no manual cast needed}Operator Precedence
Precedence decides which operator runs first when an expression has several of them.
| Level | Operators | Associativity |
|---|---|---|
| 1 | ++ -- (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 | && then || | Left to right |
| 10 | ? : | Right to left |
| 11 | = += -= *= /= %= | Right to left |
int result = 10 + 5 * 2; // 20, not 30int forced = (10 + 5) * 2; // 30When in doubt, add brackets. They cost nothing and make the intent obvious.
Common Mistakes
- Using
=instead of==inside a condition. - Expecting
10 / 3to give3.33when both operands are integers. - Comparing strings or objects with
==instead ofequals(). - Mixing
&and&&and losing short-circuit protection. - Relying on
a++ + ++astyle expressions, which are confusing to read.
double average = 10 / 3; // 3.0, division happened firstdouble correct = 10 / 3.0; // 3.333...Best Practices
- Use brackets to make precedence clear instead of memorising the table.
- Prefer
&&and||over&and|for boolean logic. - Keep ternary expressions to a single condition.
- Use
equals()for objects and==only for primitives and reference checks. - Avoid changing the same variable more than once in one expression.
Quick Summary
| Operator group | Purpose |
|---|---|
| Arithmetic | Basic maths on numbers |
| Unary | Works on one operand, including ++ and -- |
| Assignment | Stores a value, optionally with an operation |
| Relational | Compares values and returns a boolean |
| Logical | Combines conditions with short-circuit behaviour |
| Bitwise and shift | Works directly on bits |
| Ternary | Compact if-else that returns a value |
instanceof | Checks the type of an object |
Next Steps
Now that you can build expressions, the next topic is Control Flow: If-Else, Switch, and the Ternary Operator, where these conditions decide which code runs.