Operators
Operators perform actions on values
Variables store values, while operators let you do something with those values: add numbers, compare them, change a variable's value, or combine several checks. Values and operators form expressions — pieces of code that calculate a new result.
int price = 120;
int count = 3;
int total = price * count;
System.out.println(total); // 360
Here, * takes two values and calculates their product.
The result of the price * count expression is then stored in total.
Arithmetic operators
For ordinary calculations, Java uses +, -,
*, /, and %.
The last operator returns the remainder after division.
int sum = 8 + 3; // 11
int difference = 8 - 3; // 5
int product = 8 * 3; // 24
int quotient = 8 / 3; // 2
int remainder = 8 % 3; // 2
The operand types affect the result. If both numbers are integers, the result of division is also an integer: the fractional part is simply discarded. To get a fractional result, at least one operand must be a floating-point number.
double result = 8.0 / 3;
System.out.println(result); // 2.6666666666666665
8 / 3 produces 2, not 3.
Assignment and changing a value
The = operator assigns a value to a variable.
When you need to change an existing value, compound assignment operators are often more convenient:
+=, -=, *=,
/=, and %=.
int score = 10;
score += 5;
score *= 2;
System.out.println(score); // 30
To increase or decrease a number by one, Java has the ++ and
-- operators.
If such an operator is used as a statement on its own, the prefix and postfix forms produce the same final value.
int attempts = 3;
attempts++;
--attempts;
The difference appears when the result of the operation immediately participates in another expression. The prefix form changes the value first and then returns it; the postfix form returns the old value first and then changes the variable.
int value = 5;
int first = ++value;
int second = value++;
System.out.println(first); // 6
System.out.println(second); // 6
System.out.println(value); // 7
++ and -- unnecessarily complicated.
A separate line such as count++; is usually much easier to read than several changes inside one expression.
Comparison operators
A comparison does not change values — it answers a question and returns a boolean:
true or false.
int temperature = 20;
boolean warm = temperature > 15;
boolean exact = temperature == 20;
boolean different = temperature != 10;
Comparisons use <, <=,
>, >=, ==, and
!=.
= and == do different things.
One equals sign assigns a value, while two equals signs compare values.
Logical operators
Logical operators work with boolean values and let you combine several checks.
&& means “and”, || means “or”,
and ! flips a logical value to its opposite.
boolean hasTicket = true;
boolean hasDocument = false;
boolean canEnter = hasTicket && hasDocument;
boolean hasSomething = hasTicket || hasDocument;
boolean noTicket = !hasTicket;
&& and || have an important property: short-circuit evaluation.
Java evaluates the right-hand side only when it can still affect the result of the whole expression.
int value = 10;
int divisor = 0;
boolean largeResult = divisor != 0 && value / divisor > 2;
Here, after divisor != 0, it is already clear that the entire && expression is false,
so the division by zero is never performed.
Bitwise operators and shifts
Integer numbers can be viewed as sets of bits and manipulated directly.
For this, Java provides &, |, ^, and
~, along with the shifts <<,
>>, and >>>.
int flags = 0b0101;
int mask = 0b0001;
int selected = flags & mask; // 0b0001
int shifted = flags << 1; // 0b1010
& keeps a one only where both operands have a one,
while << shifts the bits to the left.
The >> operator preserves the sign when shifting right,
while >>> fills the vacated high-order bits with zeros.
In ordinary application code, these operations appear much less often than arithmetic and logical ones, but you still need to be able to read them.
The conditional ?: operator
The ?: operator chooses one of two values depending on the result of a logical expression.
It is convenient when the choice is short and really comes down to two alternatives.
boolean premium = true;
int fee = premium ? 0 : 100;
The expression before ? is evaluated first.
If it is true, the result is the value after ?;
if it is false, the result is the value after :.
Operator precedence
One expression can contain several operators. Java does not simply evaluate them from left to right: operators have precedence. For example, multiplication is performed before addition.
int first = 2 + 3 * 4; // 14
int second = (2 + 3) * 4; // 20
You do not need to memorize the entire precedence table. For obvious expressions, remembering the basic rules is enough, and when the order might be unclear, use parentheses. They both set the required evaluation order and make the intent of the code easier to see.