#1
If you're learning Java, you've probably seen things like +, -, *, or == in code already. These are called operators, and they’re a key part of how Java performs calculations, makes decisions, and processes data. Combine operators with variables and values, and you get expressions.
In this guide, we’ll walk you through what operators and expressions are in Java, the different types of operators you’ll use, and how they work in real-life code examples. By the end, you’ll have a solid understanding of how Java handles math, logic, comparisons, and more.
Let’s dive in!

What Are Operators in Java?

Operators are special symbols or keywords that tell Java to perform specific operations on variables or values.
Some examples:
  • + adds two numbers
  • == checks if two values are equal
  • && checks if two conditions are both true
Operators are used in expressions—which are combinations of variables, values, and operators that produce a result.
Example:
int result = 3 + 5;
Here, 3 + 5 is an expression, and + is the operator.

1. Arithmetic Operators

These are used for basic math operations.
Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 10 - 2 8
* Multiplication 4 * 2 8
/ Division 16 / 2 8
% Modulus (remainder) 10 % 3 1

Example:

int a = 10;
int b = 3;
System.out.println(a + b);  // 13
System.out.println(a % b);  // 1

2. Relational (Comparison) Operators

These compare two values and return a boolean (true or false).
Operator Meaning Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 7 > 2 true
< Less than 4 < 6 true
>= Greater than or equal to 5 >= 5 true
<= Less than or equal to 3 <= 2 true

Example:

int x = 10;
int y = 20;
System.out.println(x > y);   // false
System.out.println(x != y);  // true

3. Logical Operators

These are used to combine multiple boolean expressions.
Operator Meaning Example Result
&& Logical AND true && false false
` ` Logical OR
! Logical NOT !true false

Example:

int age = 25;
boolean hasID = true;

if (age > 18 && hasID) {
    System.out.println("Allowed to enter.");
}

4. Assignment Operators

These are used to assign values to variables. The most common is =, but there are shortcuts too.
Operator Meaning Example Equivalent
= Assign value x = 5
+= Add and assign x += 2 x = x + 2
-= Subtract and assign x -= 3 x = x - 3
*= Multiply and assign x *= 4 x = x * 4
/= Divide and assign x /= 2 x = x / 2
%= Modulo and assign x %= 2 x = x % 2

Example:

int x = 10;
x += 5;  // x is now 15

5. Unary Operators

These only work with one operand.
Operator Description Example
+ Unary plus +a
- Unary minus -a
++ Increment a++ or ++a
-- Decrement a-- or --a
! Logical NOT !true

Example:

int count = 5;
count++;  // Now count is 6

6. Ternary Operator

This is a shorthand for simple if-else conditions.
condition ? valueIfTrue : valueIfFalse;

Example:

int age = 18;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status);  // Adult
It’s a neat one-liner alternative to longer if-else statements.

What Is an Expression in Java?

An expression is any valid combination of variables, operators, and values that Java can evaluate to produce a result.
Examples:
int total = price * quantity;
boolean isEligible = age >= 18 && hasID;
Each of these lines contains an expression on the right-hand side that Java will evaluate.

Operator Precedence and Associativity

Sometimes expressions contain multiple operators. Java follows operator precedence to decide the order of operations (just like math).
For example:
int result = 10 + 5 * 2; // result is 20, not 30
Why? Because * (multiplication) has higher precedence than +.
If you're ever unsure, use parentheses to make it clear:
int result = (10 + 5) * 2; // now result is 30

image quote pre code