About Lesson
In Java, operators are special symbols or characters that perform specific operations on operands (variables, values, or expressions). Here’s a brief explanation of the three types of operators you asked about:
- Arithmetic Operators: These operators are used to perform mathematical operations on numeric values. The arithmetic operators in Java are:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Increment (++)
- Decrement (–)
Example:
java
int x = 10;
int y = 3;
int sum = x + y; // 13
int difference = x - y; // 7
int product = x * y; // 30
int quotient = x / y; // 3
int remainder = x % y; // 1
- Relational Operators: These operators are used to compare two values and return a boolean value (true or false) based on the comparison. The relational operators in Java are:
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Example:
java
int x = 10;
int y = 3;
boolean isEqual = x == y; // false
boolean isNotEqual = x != y; // true
boolean isGreater = x > y; // true
boolean isLess = x < y; // false
boolean isGreaterOrEqual = x >= y; // true
boolean isLessOrEqual = x <= y; // false
- Logical Operators: These operators are used to combine two or more boolean expressions and return a boolean value based on the result. The logical operators in Java are:
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)
Example:
java
boolean x = true;
boolean y = false;
boolean andResult = x && y; // false
boolean orResult = x || y; // true
boolean notResult = !x; // false