Skip to content

Operators ​

Arithmetic Operators: ​

Java supports standard arithmetic operators like +, -, *, /, % (modulo), and ++/-- (increment/decrement).

java
// Arithmetic operators
int a = 10;
int b = 3;

int sumAb = a + b;
int diffAb = a - b;
int productAb = a * b;
int quotientAb = a / b;
int remainderAb = a % b;

a++; // Equivalent to a = a + 1
b--; // Equivalent to b = b - 1

Relational Operators: ​

Relational operators are used to compare values. They return a boolean value (true or false) based on the comparison.

java
// Relational operators
int a = 10;
int b = 3;

boolean isEqual = a == b;
boolean isNotEqual = a != b;
boolean isGreaterThan = a > b;
boolean isLessThan = a < b;
boolean isGreaterThanOrEqual = a >= b;
boolean isLessThanOrEqual = a <= b;

Logical Operators: ​

Logical operators are used to combine multiple conditions and evaluate their truth values.

java
// Logical operators
boolean x = true;
boolean y = false;

boolean resultAnd = x && y;
boolean resultOr = x || y;
boolean resultNot = !x;

Assignment Operators: ​

Assignment operators are used to assign values to variables.

java
// Assignment operators
int x = 10;
int y = 5;

x += y; // Equivalent to x = x + y
x -= y; // Equivalent to x = x - y
x *= y; // Equivalent to x = x * y
x /= y; // Equivalent to x = x / y
x %= y; // Equivalent to x = x % y