java-operadores-aritmeticos-logicos-relacionales

Operators in Java: Arithmetic, Logical, and Relational

  • 4 min

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.

OperatorDescriptionExample (a=10, b=3)
+Additiona + b -> 13
-Subtractiona - b -> 7
*Multiplicationa * b -> 30
/Divisiona / 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!)
Copied!

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 double
Copied!

Increment 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).

OperatorMeaningExample
==Equality5 == 5 -> true
!=Inequality5 != 3 -> true
>Greater than5 > 10 -> false
<Less than5 < 10 -> true
>=Greater than or equal to5 >= 5 -> true
<=Less than or equal to4 <= 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.

OperatorNameDescription
&&ANDReturns true if both are true.
||ORReturns true if at least one is true.
!NOTInverts 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 is false, Java does NOT evaluate the second, because it already knows the total result will be false.
  • In an OR (||): If the first condition is true, 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
}
Copied!

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";
Copied!

Use it in moderation. It’s great for simple assignments, but if you nest multiple ternary operators (? : ? :), the code becomes unreadable.