Operators

An operator is a character that represents an action, for example + is an arithmetic operator that represents addition.

Basic Arithmetic Operators

Basic arithmetic operators are: +, -, *, /, %

  • + is for addition.

  • – is for subtraction.

  • * is for multiplication.

  • / is for division.

  • % is for modulo. Note: Modulo operator returns remainder, for example 10 % 5 would return 0

Assignment Operators

Assignments operators in java are: =, +=, -=, *=, /=, %=

  • num2 = num1 would assign value of variable num1 to the variable.

  • num2+=num1 is equal to num2 = num2+num1

  • num2-=num1 is equal to num2 = num2-num1

  • num2=num1 is equal to num2 = num2num1

  • num2/=num1 is equal to num2 = num2/num1

  • num2%=num1 is equal to num2 = num2%num1

Auto-increment and Auto-decrement operators

Auto-increment and Auto-decrement operators are: ++, --

  • num++ is equivalent to num=num+1;

  • num–- is equivalent to num=num-1;

Logical Operators

Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition. Logical operators in java are: &&, ||, !

  • b1&&b2 will return true if both b1 and b2 are true else it would return false.

  • b1||b2 will return false if both b1 and b2 are false else it would return true.

  • !b1 would return the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true.

Comparison operators

We have six relational operators in Java: ==, !=, >, <, >=, <=

  • == returns true if both the left side and right side are equal

  • != returns true if left side is not equal to the right side of operator.

  • > returns true if left side is greater than right.

  • < returns true if left side is less than right side.

  • >= returns true if left side is greater than or equal to right side.

  • <= returns true if left side is less than or equal to right side.

Bitwise Operators

Bitwise operators perform bit by bit processing. There are six bitwise Operators: &, |, ^, ~, <<, >>

Example:

- num1 = 11; /* equal to 00001011*/
- num2 = 22; /* equal to 00010110 */

Operators

  • num1 & num2 compares corresponding bits of num1 and num2 and generates 1 if both bits are equal, else it returns 0. In our case it would return: 2 which is 00000010 because in the binary form of num1 and num2 only second last bits are matching.

  • num1 | num2 compares corresponding bits of num1 and num2 and generates 1 if either bit is 1, else it returns 0. In our case it would return 31 which is 00011111

  • num1 ^ num2 compares corresponding bits of num1 and num2 and generates 1 if they are not equal, else it returns 0. In our example it would return 29 which is equivalent to 00011101

  • ~num1 is a complement operator that just changes the bit from 0 to 1 and 1 to 0. In our example it would return -12 which is signed 8 bit equivalent to 11110100

  • num1 << 2 is left shift operator that moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0. In our case output is 44 which is equivalent to 00101100

  • num1 >> 2 is right shift operator that moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. In our case output is 2 which is equivalent to 00000010

Ternary Operator

This operator evaluates a boolean expression and assign the value based on the result.

  • variable num1 = (expression) ? value if true : value if false

Operator Precedence in Java

This determines which operator needs to be evaluated first if an expression has more than one operator. Operator with higher precedence at the top and lower precedence at the bottom.

  1. Unary Operators
    ++, --, !, ~

  2. Multiplicative
    *, /, %

  3. Additive
    +, –

  4. Shift
    <<, >>, >>>

  5. Relational
    >, >=, <, <=

  6. Equality
    ==, !=

  7. Bitwise AND
    &

  8. Bitwise XOR
    ^

  9. Bitwise OR
    |

  10. Logical AND
    &&

  11. Logical OR
    ||

  12. Ternary
    ?:

  13. Assignment
    =, +=, -=, *=, /=, %=, >, >=, <, <=, &=, ^=, |=

Source:

results matching ""

    No results matching ""