Operators in Java
In Java, operators are special symbols or keywords used to perform operations on variables and values.
They are categorized based on the type of operation they perform.
1. Arithmetic Operators These operators perform basic mathematical operations:
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
• % (Modulus)
2. Relational (Comparison) Operators These operators compare two values:
• == (Equal to)
• != (Not equal to)
• > (Greater than)
• < (Less than)
• >= (Greater than or equal to)
• <= (Less than or equal to)
3. Logical Operators Used to perform logical operations:
• && (Logical AND)
• || (Logical OR)
• ! (Logical NOT)
4. Assignment Operators Used to assign values to variables:
• = (Assign)
• += , -= , *= , /= , %= (Compound assignment operators)
5. Unary Operators Operate on a single operand:
• + (Unary plus)
• - (Unary minus)
• ++ (Increment)
• -- (Decrement)
• ! (Logical NOT)
6. Bitwise Operators Perform bit-level operations:
• & (Bitwise AND)
• | (Bitwise OR)
• ^ (Bitwise XOR)
• ~ (Bitwise complement)
1
• << (Left shift)
• >> (Right shift)
• >>> (Unsigned right shift)
7. Ternary Operator A shorthand for if-else :
• ? : (Condition ? value_if_true : value_if_false)
8. instanceof Operator Used to test whether an object is an instance of a specific class or subclass:
• instanceof
9. Type Cast Operator Used to explicitly convert a value from one type to another:
• (type)
Example Code:
int a = 10, b = 20;
int max = (a > b) ? a : b; // Ternary Operator
System.out.println("Maximum: " + max);
These operators are the foundation of programming logic and expressions in Java.