Operators in Java

 

Operators in Java

 

In Java, operators are special symbols or characters that are used to perform certain operations on one or more operands. Java supports a wide range of operators, including:

  1. Arithmetic Operators: These operators are used to perform mathematical calculations on numerical values. The arithmetic operators in Java include:
    • (addition)
    • (subtraction)
    • (multiplication) / (division) % (modulus or remainder)
  2. Assignment Operators: These operators are used to assign values to variables. The assignment operators in Java include:

= (simple assignment) += (addition and assignment) -= (subtraction and assignment) *= (multiplication and assignment) /= (division and assignment) %= (modulus and assignment)

  1. Comparison Operators: These operators are used to compare two values and return a boolean value (true or false). The comparison operators in Java include:

== (equal to) != (not equal to)

(greater than) < (less than) = (greater than or equal to) <= (less than or equal to)

  1. Logical Operators: These operators are used to combine two or more boolean expressions and return a boolean value. The logical operators in Java include:

&& (logical AND) || (logical OR) ! (logical NOT)

  1. Bitwise Operators: These operators are used to perform bitwise operations on two numerical values. The bitwise operators in Java include:

& (bitwise AND) | (bitwise OR) ^ (bitwise XOR) ~ (bitwise NOT) << (left shift)

  1. Ternary Operator: This operator is used to assign one of two values to a variable depending on a boolean expression. The ternary operator in Java is denoted by the symbol "?:".

Example: int x = (a > b) ? a : b; // assigns the larger of a and b to x.

These are some of the commonly used operators in Java.

Java Operator Precedence

 Java operator precedence determines the order in which operators are evaluated in an expression. In Java, operators with higher precedence are evaluated first. If operators have the same precedence, they are evaluated from left to right. Here is a summary of Java operator precedence, from highest to lowest:

 



Operator Type

Category

Precedence

Unary

postfix

expression ++ expression--

prefix

++expression --expression +expression -expression ~ !

Arithmetic

multiplicative

* / %

additive

+ -

Shift

shift

<< >> >>>

Relational

comparison

< > <= >= instanceof

equality

== !=

Bitwise

bitwise AND

&

bitwise exclusive OR

^

bitwise inclusive OR

|

Logical

logical AND

&&

logical OR

||

Ternary

ternary

? :

Assignment

assignment

= += -= *= /= %= &= ^= |= <<= >>= >>>=



Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to override the default precedence order and specify the order of evaluation.









Java Unary Operator


In Java, a unary operator is an operator that operates on a single operand. There are several unary operators in Java:

  1. Unary Plus (+): It is used to indicate a positive value. For example, +5 is positive 5.
  2. Unary Minus (-): It is used to indicate a negative value. For example, -5 is negative 5.
  3. Logical Complement (!): It is used to invert the value of a boolean expression. For example, if x is true, then !x is false.
  4. Bitwise Complement (~): It is used to invert the bits of an integer. For example, if x is 5 (binary 0101), then ~x is -6 (binary 1010).
  5. Pre-increment (++x): It is used to increment the value of a variable before it is used in an expression. For example, if x is 5, then ++x is 6.
  6. Pre-decrement (--x): It is used to decrement the value of a variable before it is used in an expression. For example, if x is 5, then --x is 4.
  7. Post-increment (x++): It is used to increment the value of a variable after it is used in an expression. For example, if x is 5, then x++ is 5 and the value of x becomes 6.
  8. Post-decrement (x--): It is used to decrement the value of a variable after it is used in an expression. For example, if x is 5, then x-- is 5 and the value of x becomes 4.

Unary operators have a higher precedence than binary operators in Java, except for the postfix increment and decrement operators which have the same precedence as other binary operators.
























 

 

Post a Comment

0 Comments