Operators in Java Explained: Arithmetic, Relational, and Logical
Operators in Java
Operators are special symbols used to perform operations on variables and values in Java. Java provides several types of operators to perform arithmetic, logical, relational, and other operations.
1. Arithmetic Operators
Used to perform basic mathematical operations.
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus (remainder)
2. Relational Operators
Used to compare two values and return a boolean result (true or false).
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to
- == Equal to
- != Not equal to
3. Compound Assignment Operators
Used to perform an operation and assign the result to the same variable.
- += Add and assign
- -= Subtract and assign
- *= Multiply and assign
- /= Divide and assign
- %= Modulus and assign
4. Unary Operators
Used to operate on a single operand.
- ++ Increment
- -- Decrement
- ! Logical negation
- Increment Operators
- Post-increment (a++) – Value is used first, then increased
- Pre-increment (++a) – Value is increased first, then used
- Decrement Operators
- Post-decrement (a--) – Value is used first, then decreased
- Pre-decrement (--a) – Value is decreased first, then used
5. Ternary Operator
Used to check a condition and return one of two values.
Syntax:
- condition ? value1 : value2;
6. Conditional (Logical) Operators
Used to combine multiple conditions.
- && Logical AND
7. Bitwise Operators
Used to perform operations at the bit level.
- & Bitwise AND
- ^ Bitwise XOR
- ~ Bitwise NOT (Negation)
Comments
Post a Comment