Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Some of the types are-
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
- instance of operator
- Precedence and Associativity
- Interesting Questions
Lets take a look at them in detail.
- Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data types.
- * : Multiplication
- / : Division
- % : Modulo
- + : Addition
- – : Subtraction
- Unary Operators: Unary operators need only one operand. They are used to increment, decrement or negate a value.
- – :Unary minus, used for negating the values.
- + :Unary plus, used for giving positive values. Only used when deliberately converting a negative value to positive.
- ++ :Increment operator, used for incrementing the value by 1. There are two varieties of increment operator.
- Post-Increment : Value is first used for computing the result and then incremented.
- Pre-Increment : Value is incremented first and then result is computed.
- — : Decrement operator, used for decrementing the value by 1. There are two varieties of decrement operator.
- Post-decrement : Value is first used for computing the result and then decremented.
- Pre-Decrement : Value is decremented first and then result is computed.
- ! : Logical not operator, used for inverting a boolean value.
- Assignment Operator : ‘=’ Assignment operator is used to assign a value to any variable. It has a right to left associativity, i.e value given on right hand side of operator is assigned to the variable on the left and therefore right hand side value must be declared before using it or should be a constant.
General format of assignment operator is,
variable = value;
- In many cases assignment operator can be combined with other operators to build a shorter version of statement called Compound Statement. For example, instead of a = a+5, we can write a += 5.
- +=, for adding left operand with right operand and then assigning it to variable on the left.
- -=, for subtracting left operand with right operand and then assigning it to variable on the left.
- *=, for multiplying left operand with right operand and then assigning it to variable on the left.
- /=, for dividing left operand with right operand and then assigning it to variable on the left.
- %=, for assigning modulo of left operand with right operand and then assigning it to variable on the left.
- Relational Operators : These operators are used to check for relations like equality, greater than, less than. They return boolean result after the comparison and are extensively used in looping statements as well as conditional if else statements. General format is,
variable relation_operator value
- Some of the relational operators are-
- ==, Equal to : returns true if left hand side is equal to right hand side.
- !=, Not Equal to : returns true if left hand side is not equal to right hand side.
- <, less than : returns true if left hand side is less than right hand side.
- <=, less than or equal to : returns true if left hand side is less than or equal to right hand side.
- >, Greater than : returns true if left hand side is greater than right hand side.
- >=, Greater than or equal to: returns true if left hand side is greater than or equal to right hand side.
- Logical Operators : These operators are used to perform “logical AND” and “logical OR” operation, i.e. the function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision.
Conditional operators are-
- &&, Logical AND : returns true when both conditions are true.
- ||, Logical OR : returns true if at least one condition is true.
- Ternary operator : Ternary operator is a shorthand version of if-else statement. It has three operands and hence the name ternary. General format is-
condition ? if true : if false
The above statement means that if the condition evaluates to true, then execute the statements after the ‘?’ else execute the statements after the ‘:’.
- Bitwise Operators : These operators are used to perform manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing update and query operations of Binary indexed tree.
- &, Bitwise AND operator: returns bit by bit AND of input values.
- |, Bitwise OR operator: returns bit by bit OR of input values.
- ^, Bitwise XOR operator: returns bit by bit XOR of input values.
- ~, Bitwise Complement Operator: This is a unary operator which returns the one’s compliment representation of the input value, i.e. with all bits inversed.
- Shift Operators : These operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively. They can be used when we have to multiply or divide a number by two. General format-
number shift_op number_of_places_to_shift;
- <<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two.
- >>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of initial number. Similar effect as of dividing the number with some power of two.
- >>>, Unsigned Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit is set to 0.
- instance of operator : Instance of operator is used for type checking. It can be used to test if an object is an instance of a class, a subclass or an interface. General format-
object instance of class/subclass/interface
obj1 instanceof Person: true obj1 instanceof Boy: false obj1 instanceof MyInterface: false obj2 instanceof Person: true obj2 instanceof Boy: true obj2 instanceof MyInterface: true
Precedence and Associativity of Operators
Precedence and associative rules are used when dealing with hybrid equations involving more than one type of operator. In such cases, these rules determine which part of the equation to consider first as there can be many different valuations for the same equation. The below table depicts the precedence of operators in decreasing order as magnitude with the top representing the highest precedence and bottom shows the lowest precedence.
Interesting Questions on Operators
- Precedence and Associativity: There is often a confusion when it comes to hybrid equations that is equations having multiple operators. The problem is which part to solve first. There is a golden rule to follow in these situations. If the operators have different precedence, solve the higher precedence first. If they have same precedence, solve according to associativity, that is either from right to left or from left to right. Explanation of below program is well written in comments withing the program itself.
a+b/d = 20 a+b*d-e/f = 219
- Be a Compiler:Compiler in our frameworks utilizes lex device to coordinate the best match while creating tokens. This makes somewhat of an issue whenever ignored. For instance, consider the statement a=b+++c;, to a large number of the perusers this may appear to make compiler blunder. Be that as it may, this assertion is totally right as the token made by lex are a, =, b, ++, +, c. Accordingly this assertion has a comparative impact of first appointing b+c to an and afterward increasing b. Additionally, a=b+++++c; would produce blunder as tokens created are a, =, b, ++, ++, +, c. which is really a mistake as there is no operand after second unary operand.
Value of a(b+c), b(b+1), c = 10, 11, 0
- Using + over (): When utilizing + administrator inside system.out.println() try to do expansion utilizing enclosure. In the event that we compose something prior to doing option, at that point string option happens, that is associativity of option is left to right and subsequently whole numbers are added to a string initially delivering a string, and string objects connect when utilizing +, hence it can make undesirable outcomes.
Concatenation (x+y)= 58 Addition (x+y) = 13
This article is contributed by Rishabh Mahrsee. In the event that you like GeeksforGeeks and might want to contribute, you can likewise compose an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article showing up on the GeeksforGeeks fundamental page and help different Geeks.
Kindly compose remarks on the off chance that you discover anything erroneous, or you need to share more information about the subject examined previously.
Consideration peruser! Try not to quit adapting now. Get hold of the multitude of important Java Foundation and Collections ideas with the Fundamentals of Java and Java Collections Course at an understudy benevolent cost and become industry prepared.