An operator is a symbol that combines, transforms, or compares one or more values.
We already know how to create variables and even let Java infer their types. A variable by itself is just data stored in a box; programming is about doing things with that data.
For that, we use operators.
Arithmetic Operators
These are the classic ones. They are used for basic math.
| Operator | Description | Example (a=10, b=3) |
|---|---|---|
+ | Addition | a + b -> 13 |
- | Subtraction | a - b -> 7 |
* | Multiplication | a * b -> 30 |
/ | Division | a / b -> 3 (Watch out!) |
% | Modulus (Remainder) | a % b -> 1 |
The Integer Division Trap
Notice the example in the table: 10 / 3 gives 3, not 3.3333.
In Java, if you divide two integers, the result is an integer. The decimals are truncated. It doesn’t matter if you store the result in a double.
int a = 5;
int b = 2;
double resultado = a / b;
System.out.println(resultado); // Prints 2.0 (Oops!)To get decimals, at least one of the operands must be a decimal (float or double).
double resultado = (double) a / b; // Explicit casting
// Or the quick trick:
double resultado2 = a / 2.0; // Dividing by a double literal makes the result doubleIncrement and Decrement (++, --)
Widely used in loops. They increase or decrease the variable by 1.
a++(Post-increment): Use the value, then increment.++a(Pre-increment): Increment, then use the value.
Relational Operators
They are used to compare two values. The result of these operations is always a boolean (true or false).
| Operator | Meaning | Example |
|---|---|---|
== | Equality | 5 == 5 -> true |
!= | Inequality | 5 != 3 -> true |
> | Greater than | 5 > 10 -> false |
< | Less than | 5 < 10 -> true |
>= | Greater than or equal to | 5 >= 5 -> true |
<= | Less than or equal to | 4 <= 5 -> true |
Danger with Objects!
The == operator compares values for primitives. For objects (like String), it checks if both references point to the same object.
To compare the content of a text, use .equals() instead of ==. We will cover this in depth in the strings topic.
Logical Operators
They allow combining multiple boolean conditions. They are the foundation of complex decision-making.
| Operator | Name | Description |
|---|---|---|
&& | AND | Returns true if both are true. |
|| | OR | Returns true if at least one is true. |
! | NOT | Inverts the value (!true is false). |
Short-Circuit Evaluation
This is a very important technical feature of Java (and C++).
- In an AND (
&&): If the first condition isfalse, Java does NOT evaluate the second, because it already knows the total result will be false. - In an OR (
||): If the first condition istrue, Java does NOT evaluate the second, because it already knows the total result will be true.
This helps write safe code and avoid errors:
// Imagine 'usuario' is null.
// If short-circuit didn't exist, usuario.esAdmin() would throw an error.
if (usuario != null && usuario.esAdmin()) {
// We enter here safely
}The Ternary Operator (? :)
It is the only operator that takes three operands. It is basically a condensed if-else in a single line.
Syntax: condition ? value_if_true : value_if_false
int edad = 18;
String estado = (edad >= 18) ? "Adult" : "Minor";Use it in moderation. It’s great for simple assignments, but if you nest multiple ternary operators (? : ? :), the code becomes unreadable.