11. Types of Operator part1
1. Arithmetic Operators
These are used to perform basic mathematical operations.
Addition (+): Adds two numbers.
int a = 5; int b = 3; int sum = a + b; // sum is 8Subtraction (-): Subtracts one number from another.
int difference = a - b; // difference is 2Multiplication (*): Multiplies two numbers.
int product = a * b; // product is 15Division (/): Divides one number by another.
int quotient = a / b; // quotient is 1Modulus (%): Gives the remainder of a division.
int remainder = a % b; // remainder is 2
2. Relational Operators
These are used to compare two values.
Equal to (==): Checks if two values are equal.
boolean isEqual = (a == b); // isEqual is falseNot equal to (!=): Checks if two values are not equal.
boolean isNotEqual = (a != b); // isNotEqual is trueGreater than (>): Checks if one value is greater than another.
boolean isGreater = (a > b); // isGreater is trueLess than (<): Checks if one value is less than another.
boolean isLess = (a < b); // isLess is falseGreater than or equal to (>=): Checks if one value is greater than or equal to another.
boolean isGreaterOrEqual = (a >= b); // isGreaterOrEqual is trueLess than or equal to (<=): Checks if one value is less than or equal to another.
boolean isLessOrEqual = (a <= b); // isLessOrEqual is false.
3. Logical Operators
These are used to combine multiple boolean expressions.
AND (&&): Returns true if both conditions are true.
boolean result = (a > 2 && b < 5); // result is trueOR (||): Returns true if at least one condition is true.
boolean result = (a > 5 || b < 5); // result is true**NOT (!) **: Reverses the logical state.
boolean result = !(a > b); // result is false
4. Bitwise Operators
These operate on binary representations of integers.
AND (&): Performs a bitwise AND.
int result = a & b; // result is 1 (binary 0101 & 0011 = 0001)OR (|): Performs a bitwise OR.
int result = a | b; // result is 7 (binary 0101 | 0011 = 0111)XOR (^): Performs a bitwise exclusive OR.
int result = a ^ b; // result is 6 (binary 0101 ^ 0011 = 0110)NOT (~): Performs a bitwise NOT.
int result = ~a; // result is -6 (binary 0101 = 1010)Left Shift (<<): Shifts bits to the left.
int result = a << 1; // result is 10 (binary 0101 << 1 = 1010)Right Shift (>>): Shifts bits to the right.
int result = a >> 1; // result is 2 (binary 0101 >> 1 = 0010)
5. Assignment Operators
These are used to assign values to variables.
- Simple assignment (=): Assigns the right-side value to the left-side variable.
int c = a; // c is 5
Comments
Post a Comment