C OPERATORS

Operators: Operators are special symbol that perform specific operation on one, two or three operands and then return a result.

Operand: Operand is a term used to describe any object that is manipulated by an operator.

Example:

int value = 15+12;

Here ‘+’ is the operator and 15 & 12 are operand.

There are many operators in Java, classified according to there features:

  • Arithmetic Operator
  • Unary Operator
  • Binary Operator
  • Shift Operator
  • Relational Operator
  • Bitwise Operator
  • Logical Operator
  • Ternary Operator
  • Assignment Operator

1. Arithmetic Operator: They are used to perform five basic arithmetic operations:

  • + : Addition
  • – : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus

Arithmetic operators are further divided into two types:

  • Unary Operators
  • Binary Operators

2. Unary Operator: They performs operations on single operand.

  • Unary minus(-): It is  used for reversing the sign of the operand’s value.
  • Unary plus(+): It  indicates that the value is positive (however numbers are considered positive without this operator).
  • Increment operator(++):  This operator adds 1 to its operand. This operator is in two variant:
    • Post-Increment: In this the value is first used for computing the result and then the value is incremented by 1.
    • Pre-Increment: In this the value is incremented first by 1, and then the result is computed.
  • Decrement operator(–):This operator substracts 1 from its operand. This operator is in two variant:
    • Post-Decrement: In this the value is first used for computing the result and then the value is decremented by 1.
    • Pre-Decrement: In this the value is decremented first by 1, and then the result is computed.
  • Logical not operator(!): It is used for inverting a Boolean value.

3. Binary Operator: They performs operations on two operands.

  • Addition Operator(+)
  • Subtraction Operator(-)
  • Multiplication Operator(*)
  • Division Operator(/)
  • Modulus Operator(%)

4. Relational Operator: The relational operator determines the relationship among two operands.

The output results in Boolean value.

 

Operator Meaning of Operator Example
> Greater than (2>1) returns True
< Less than (2<1) returns False
>= Greater than equal to (2>=1) returns True
<= Less than equal to (2<=1) returns False
== Equal to (2==1) returns False
!= Not equal to (2!=1) returns True

5. Logical Operator: Logical operators are used to determine the logic between variables or value.

Operator Meaning Example
&&

Logical AND

(Returns True if both statements are true)

If a = 2 and b = 3 then, expression ((a==2) && (b==2)), returns False.
||

Logical OR

(Returns True if one of the statement is true)

If a = 2 and b = 3 then, expression ((a==2) || (b==2)), returns True.
!

Logical NOT

(Reverse the result, returns false if the result is true)

If a = 5 then, expression !(a==5) returns False.

6. Bitwise Operator: Bitwise operator works on bit and performs bit-by-bit operations.

Operator Description
&

Bitwise AND

Copies a bit to the result if it exists in both operands.

|

 Bitwise OR

Copies a bit to the result if it exists in either operands.

^

 Bitwise XOR

Copies a bit if it is set in one operand but not both.

7. Shift Operator: A shift operator performs bit manipulation on data by shifting the bits of its first operand right or left.

Operator Description
<<

Left Shift (A<<B)

The left operand value is moved left by the number of bits specified by the right operand.

>>

Right Shift (A>>B)

The left operand value is moved right by the number of bits specified by the right operand.

>>>

Zero fill Right Shift (A>>>B)

The left operand value is moved right by the number of bits specified by the right operand and shifted values are filled up with zero.

8. Assignment Operator: Assignment operators are used to assign values to the operators.

Operator Meaning of Operator Example
=

Simple Assignment

Assigns values from right side operands to left side operand

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

Add AND Assignment

It adds right operand to left operand and assigns the result to left side operand

C+=A  =>  C = C+A
-=

Substract AND Assignment

It substract right operand from the left operand and assigns the result to left side operand

C-=A  =>  C = C-A
*=

Multiply AND Assignment

It multiplies right operand with the left operand and assigns the result to left side operand

C*=A  =>  C = C*A
/=

 Divide AND Assignment

It divides left operand with the right operand and assigns the result to left side operand

C/=A  =>  C = C/A
%=

Modulus AND Assignment

It modulus two operands and assigns the result to left side operand

C%=A  =>  C = C%A

5. Logical Operator: Logical operators are used to determine the logic between variables or value.

Operator Meaning Example
&&

Logical AND

(Returns True if both statements are true)

If a = 2 and b = 3 then, expression ((a==2) && (b==2)), returns False.
||

Logical OR

(Returns True if one of the statement is true)

If a = 2 and b = 3 then, expression ((a==2) || (b==2)), returns True.
!

Logical NOT

(Reverse the result, returns false if the result is true)

If a = 5 then, expression !(a==5) returns False.

9. Conditional Operator: Conditional operator is also known as ternary operator.

This operator requires 3 operands.

Syntax:

result = expresion1? expresion2:expresion3;

Example:

result = (marks >= 50)? "True":"False";

Precedence and Associativity of operators.

Precedence: Precedence defines the order in which given mathematical expression is evaluated.

Operators with higher precedence is evaluated first and operators with lower precedence is evaluated at last.

Example:

(+,-) has less precedence compared to (*,/), so (*,/) are evaluated first.

Associativity: Operators with same precedence follow operator associativity.

Associativity is the direction of execution of operator.  It can either be left to right or right to left.

Example:

*  /        =>    L to R

+  –       =>    L to R

++  =    =>   R to L

 

Operator Precedence and Associativity
Operators Precedence Associativity
postfix increment and decrement ++ — left to right
prefix increment and decrement, and unary ++ — + – ~ ! right to left
multiplicative * / % left to right
additive + – left to right
shift << >> >>> left to right
relational < > <= >= instanceof left to right
equality == != left to right
bitwise AND & left to right
bitwise exclusive OR ^ left to right
bitwise inclusive OR | left to right
logical AND && left to right
logical OR || left to right
ternary ? : right to left
assignment = += -= *= /= %=
&= ^= |= <<= >>= >>>=
right to left

Resulting data type after arithmetic:

byte + short = int

short + int = int

long + float = float

char + int = int 

char + short = int 

long + double = double

float + double = double