Programming





Operators in Java

Operators in Java

by | 23-Mar-2020
Java

Tags:

Share: 


What is an Operator?

An operator is usually an instruction given to the computer. In mathematics we use +, -, x, etc to perform various operations. Then those +, -, x are different types of operators and they have been assigned with different tasks like addition, subtraction, multiplication respectively. For e.g.

A + B = C

Here + and = symbols are two operators. 

What is an Operand?

The alphabets or numbers which are used to perform the instruction are called as an operand. In the above example A, B, and C are the operands. Depending on the number of operands there are three types of operators defined in Java:

  1. Unary operator: Requires only one operand
  2. Binary operator: Requires two operands
  3. Ternary operator: Requires three operands

 Here is a list of different types of operators in Java:

  1. Arithmetic operator
    1. Unary operator
    2. Binary operator
  2. Assignment operator
  3. Relational operator
  4. Logical operator
  5. Bitwise operator
  6. instanceof operator
  7. Conditional operator
  8. Increment/Decrement operator
  9. Comparison Operator
  10. Ternary Operator
  11. Shift Operator

Explanation in Detail

  1. Arithmetic Operator
    1. Unary Operator: Only one operand is required
      1. Increment operator
        1. Post Increment Operator
        2. Pre Increment Operator
      2. Decrement operator
        1. Post decrement Operator
        2. Pre decrement Operator
      3. Logical Not operator
      4. Operator

        Description

        Example

        ++

        Increases the value of operand by 1

        A++ is equivalent to A = A+1

        --

        Decreases the value of operand by 1

        A-- is equivalent to A = A11

         

        ! (NOT)

        Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false

        !A is true

    2.  

    3. Binary Operator: Two operators are required to perform a binary operation.
      1. Addition & Concatenation
      2. Subtraction
      3. Multiplication
      4. Division
      5. Modulus
  1. Assignment Operator 

Operator

Description

Example

=

Simple assignment operator, Assigns values from right side operands to left side operand

C = A + B will assign value of  A + B into C

+=

Addition and assignment operator. It adds right operand to the left operand and assigns the result to left operand

A += B is equivalent to A = A + B

-=

Subtract and assignment operator, It subtracts right operand from the left operand and assigns the result to left operand

A -= B is equivalent to A = A – B

*=

Multiply and assignment operator, It multiplies right operand with the left operand and assigns the result to left operand

A *= B is equivalent to A = A * B

/=

Divide and assignment operator, It divides left operand with the right operand and assigns the result to left operand

A /= B is equivalent to A = A / B

%=

Modulus and assignment operator, It takes modulus using two operands and assign the result to left operand

A %= B is equivalent to A = A % B

<<=

Left shift and assignment operator

A <<= 2 is same as A = A << 2

>>=

Right shift and assignment operator

A >>= 2 is same as A = A >> 2

&=

Bitwise and assignment operator

A &= 2 is same as A = A & 2

^=

bitwise exclusive OR and assignment operator

A ^= 2 is same as A = A ^ 2

|=

bitwise inclusive OR and assignment operator

A |= 2 is same as A = A | 2

  1. Relational Operator 

Operator

Description

Example

==

Checks if the value of two operands is equal, if yes then condition becomes true else it is false.

(A == B) is not true

!=

Checks if the value of two operands is not equal, if values are not equal then the condition becomes true.

(A != B) is true

Checks if the value of the left operand is greater than the value of right operand, if yes then condition becomes true.

(A > B) is not true

Checks if the value of the left operand is less than the value of right operand, if yes then condition becomes true.

(A < B) is true

>=

Checks if the value of the left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

(A >= B) is not true

<=

Checks if the value of the left operand is less than or equal to the value of right operand, if yes then condition becomes true.

(A <= B) is true

  1. Logical Operator

Operator

Description

Example

&& (AND)

Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.

(A && B) is false.

|| (OR)

Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true.

(A || B) is true.

 

! (NOT)

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

!A is true.

  1. Bitwise Operator 

Operator

Description

Example

&

Binary AND Operator copies a bit to the result if it exists in both operands.

(A & B) will give 12 which is 0000 1100

|

Binary OR Operator copies a bit if it exists in either operand.

(A | B) will give 61 which is

0011 1101

^

Binary XOR Operator copies the bit if it is set in one operand but not both.

(A ^ B) will give 49 which is 0011 0001

^

Binary XOR Operator copies the bit if it is set in one operand but not both.

(A ^ B) will give 49 which is 0011 0001

~

Binary One's Complement Operator is unary and has the effect of 'flipping' bits.

(~A ) will give -60 which is 1100 0011

<< 

Binary Left Shift Operator. The left operand's value is moved left by the number of bits specified by the right operand.

A << 2 will give 240 which is 1111 0000

>> 

Binary Right Shift Operator. The left operand's value is moved right by the number of bits specified by the right operand.

A >> 2 will give 15 which is 1111

>>> 

Shift right zero-fill operators. The left operands value is moved right by the number of bits specified by the right

An operand and shifted values are filled up with zeros.

A >>>2 will give 15 which

is 0000 1111

  1. instanceof Operator

instanceof operator will return true if both the objects are of the same type. It works for class, subclass, and interface types. It is used to check if the reference and the specified type have some inheritance (is-a) relation. Hence it is also called a type comparison operator. instanceof is also a keyword in java. This operator is a binary operator. The left side operand must be a reference and the right side operand must be a type name.

  1. Conditional operator

It is used to return the value based on some condition. For e.g.:

<booleanValue>?<value1>:<value2>

The first operand should result in a Boolean type. If the Boolean value is true then the value1 will be returned otherwise value2 will be returned. The type of value1 and value2 must be assignment compatible with the variable where the returned value will be stored. It can be used as a replacement for the if-else condition.

Java Operator Precedence

Operator precedence means if there is more than one operator in an expression then which expression will be solved first and what will be the direction of solving the expression. It is similar to BODMAS rule in mathematics. This is a tabular representation of the precedence and associativity of Java Operators. 

Precedence*

Operator

Type

Associativity

15

()
[]
·

Parentheses
Array subscript
Member selection

Left to Right

14

++
--

Unary post-increment
Unary post-decrement

Right to left

13

++
--
+
-
!
~
type )

Unary pre-increment
Unary pre-decrement
Unary plus
Unary minus
Unary logical negation
Unary bitwise complement
Unary type cast

Right to left

12

*
/
%

Multiplication
Division
Modulus

Left to right

11

+
-

Addition
Subtraction

Left to right

10

<<
>>
>>>

Bitwise left shift
Bitwise right shift with sign extension
Bitwise right shift with zero extension

Left to right

9

<
<=
>
>=
instanceof

Relational less than
Relational less than or equal
Relational greater than
Relational greater than or equal
Type comparison (objects only)

Left to right

8

==
!=

Relational is equal to
Relational is not equal to

Left to right

7

&

Bitwise AND

Left to right

6

^

Bitwise exclusive OR

Left to right

5

|

Bitwise inclusive OR

Left to right

4

&&

Logical AND

Left to right

3

||

Logical OR

Left to right

2

? :

Ternary conditional

Right to left

1

=
+=
-=
*=
/=
%=

Assignment
Addition assignment
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment

Right to left

*Larger number means higher precedence.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.