1.

Operators in Java

Answer»

The below image shows the different types of operators available in Java. 

The following table describes each category of operators along with examples of each type.

Operator TypeDescriptionOperators
Unary OperatorsOnly one operand is required for unary operators. They are used to increase, decrease, or negate a value.++, --, +, -, !
Arithmetic OperatorsSimple arithmetic operations on primitive data types are performed with them.+, – , *, / , %
Assignment OperatorsThe assignment operator is used to give any variable a value. It has right-to-left associativity, which means that the value given on the right-hand side of the operator is assigned to the variable on the left. As a result, the right-hand side value must be declared before use or be a constant.=, +=, -=, *=, /=, %=, &=, ^=,
Relational OperatorsThese operators are used to verify for equality, greater than, and less than relationships. They return a boolean result following comparison and are frequently used in looping and conditional if-else statements.>, <, >=, <=, ==, !=
Logical OperatorsThese operators conduct “logical AND” and “logical OR” operations, which are analogous to the AND and OR gates in digital electronics. One point to bear in mind is that if the first condition is false, the second condition is not examined, resulting in a short-circuiting effect. Used to test for a variety of variables before making a choice.&&, ||
Ternary OperatorThe ternary operator is a condensed form of the if-else expression. The name ternary comes from the fact that it has three operands.?:
Bitwise OperatorsThese operators are used to manipulate the bits of a number individually. They can be applied to any integer type. They’re used to do Binary indexed tree updates and query operations.&, |, ^
Shift OperatorsThese operators shift the bits of a number to the left or right, multiplying or dividing it by two, accordingly. When we need to multiply or divide a number by two, we can use them.<<, >>, >>>

Precedence and Associativity of operators in Java:- When dealing with hybrid equations involving more than one type of operator, precedence and associative principles are applied. Because there might be multiple valuations for the same equation, these criteria determine which component of the equation to analyse first. The precedence of operators is shown in the table below in decreasing order of magnitude, with the highest precedence at the top and the lowest precedence at the bottom.

OperatorsAssociativityType
++, –Right to LeftUnary postfix
++, --, +, -, !Right to LeftUnary prefix
/, *, %Left to RightMultiplicative
+, -Left to RightAdditive
<, <=, >, >=Left to RightRelational
==, !==Left to RightEquality
&Left to RightBoolean Logical AND
^Left to RightBoolean Logical Exclusive OR
|Left to RightBoolean Logical Inclusive OR
&&Left to RightConditional AND
||Left to RightConditional OR
?:Right to LeftConditional
=, +=, -=, *=, /=, %=Right to LeftAssignment



Discussion

No Comment Found