Java ยท Chapter 9 of 42
Java Operators
Operators perform operations on variables and values. Java supports arithmetic (+, -, *, /, %), assignment (=, +=, -=), comparison (==, !=, <, >), and logical (&&, ||, !) operators.
The % (modulus) operator returns the remainder of division, and is very useful for tasks like checking even/odd numbers.
Syntax
a + b
a == b
a && bArithmetic and assignment
Basic math operators combine with = to form compound assignment operators like +=, -=, *=, /= for shorthand updates.
Comparison and logical
Comparison operators return a boolean. Logical operators && (AND), || (OR) and ! (NOT) combine boolean expressions.
Example 1 (java)
public class Main {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(a + b);
System.out.println(a % b);
System.out.println(a > b && b > 0);
}
}Output
13
1
trueArithmetic, modulus, and a logical AND expression are evaluated.
Key points
- % returns the remainder of division.
- Comparison operators return boolean values.
- && is AND, || is OR, ! is NOT.
- Compound assignment like += shortens common updates.
๐ก Note: Java uses short-circuit evaluation for && and ||, so the second operand may not be evaluated.
