Q1: What are the operators?
Q3: Arithmetic operators?
Q4: Assignment Operators?
Q5: Relational Operators?
Q6: Logical Operators?
Q7: Unary Operators?
Q8: Bitwise Operators?
Q9: instanceof Operator?
Q10: Ternary Operator?
Q11: Precedence and Associativity of Operators
1. What are the operators:-
-symbols that perform operations on variables and values. For example, + is used to perform an addition operation.
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
- instanceof Operator
- Ternary Operator
3. Arithmetic Operators:-
- these operators are used to perform basic mathematical operations on variables or data. For example, a+b or so
- Addition : a+b, here, a is added with b variable
- Subtraction : a-b, here, b is subtracted from b variable
- Multiplication : a*b, here, a is multiplied wth b variable
- Division : a/b, here, b is divided by a
- Modulus : a%b, here, remainder after division of b by a is passes
4. Assignment Operators:-
-used to store the value in variable right after performing basic mathematical operations
- = : Assignment, used to assign values to variables. a=6 will store 6 value in a variable. It assigns the value on it's the right side to it's left.
- += : Addition Assignment, a += b will work as a = a + b;
- -= : Subtraction Assignment, a -= b; will work as a = a - b;
- *= : Multiplication Assignment, a *= b; will work as a = a * b;
- /= : Division Assignment, a /= b; will work as a = a / b;
- %= : Modulus Assignment, a %= b; will work as a = a % b;
5. Relational Operators:-
- == : Is Equal To, 3 == 9 returns false
- != : Not Equal To, 8 != 5 returns true
- > : Greater Than, 1 > 5 returns false
- < : Less Than, 1 < 5 returns true
- >= : Greater Than or Equal To, 1 >= 5 returns false
- <= : Less Than or Equal To, 1 <= 5 returns false
6. Logical Operators:-
- && : logical AND, expression1 && expression2, return true only if both expressions are true
- || : logical OR, expression1 || expression2, return true if at least one expression is true
- ! : logical NOT, !expression, return true if expression is not true or false
7. Unary Operators:-
-these operators are used only with one operand.
- + : Unary plus, stores value as positive, but there is no need to use it since without any operator the value is taken as positive
- - : Unart minus, convert a negative value to a positive one, a = -10
- ! : Logical complement operator, inverts the value of a boolean,
- ++ : Increment operator, increments value by 1
- -- : Decrement operator, decrements value by 1
NOTE: please check Java: Increment, Decrement operator for detailed ++ and --.
8. Bitwise Operators:-
-used to perform operations on individual bits
-can be used with any of the integer types
- ~ : Bitwise Complement, return 1n's complement
- & : Bitwise AND. returns bit by bit AND of input values
- | : Bitwise OR, returns bit by bit OR of input values
- ^ : Bitwise XOR, returns bit by bit XOR of input values
- << : Left Shift Operator, shifts all bits towards the left by a certain number of specified bits. For example, number<<2 where 2 means 2-bit operation
- <<< : Unsigned Left shift operator, since "<<<" and "<<" are similar, there is no "<<<" operator in java.
- >> : Signed Right Shift Operator, shifts all bits towards the right by a certain number of specified bits, For example, number>>2 where 2 means 2-bit operation
- >>> : Unsigned Right Shift Operator
-checks whether an object is an instanceof a particular class
-For example, output = str instanceof String;
10. Ternary Operator:-
-short trick for "if-then-else" statement
-Syntax: variable = expression ? expression1 : expression2
OR you can also say,
variable = condition ? if_execution_block: else_execution_block
11. Precedence and Associativity of Operators:-
-higher it appears in the table, the higher its precedence
-If an expression has more than one operators with similar precedence, the expression is evaluated according to its associativity i.e. left to right or right to left. For example, x=y=z=a, here allocation will be followed from right to left.
-General doubts on Precedence and Associativity:
- using + in System.out.println(): in order to perform addition operation in System.out.println(), one should make sure to add them in parenthesis. Now the question arises why? Well, associativity of addition is left to right and hence integers are added to a string first and so forth.
- is something like a=n+++o right? the answer is yes because it is understood by the compiler as a, =, n, ++, +, 0. But something like a=b+++++c will not work since there is no operand after the second unary operator.
Please comment if you have any feedback or suggestions