Java Operators & Expressions
Master mathematical, relational, and logical operators in Java to perform complex expressions.
Java Operators
Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.
1. Arithmetic Operators
Used to perform basic mathematical operations.
+(Addition)-(Subtraction)*(Multiplication)/(Division)%(Modulo/Remainder)
2. Relational Operators
These operators compare two values and return a boolean (true/false).
int a = 10;
int b = 20;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // false3. Logical Operators
Used to combine multiple boolean expressions.
&&(Logical AND): True if both operands are true.||(Logical OR): True if either operand is true.!(Logical NOT): Reverses the logical state of its operand.
4. The Ternary Operator
A shorthand for the if-else statement. Syntax: condition ? ifTrue : ifFalse
int max = (a > b) ? a : b;