Tuesday, 27 November 2018

Java - Operators


In general operators can be divided into three categories
·      Unary – requires one operand
·      Binary – requires two operand
·      Ternary – requires three operand
Operators can be used to provide meaning between two or more operands, expressions.
Relational operators check the comparison between two expressions and always returns true or false.
Boolean logical operators check the comparison between two or more relational expressions.
Bitwise operators work with bits on given expressions.
Some Examples
·      a>b -> Relational expression
·      ((a>b) & (a>c)) -> Logical expression
·      (a&b) -> Bitwise expression
·      (a+b) -> Arithmetic expression
Types of operators.
·      Basic arithmetic operator
·      Arithmetic assignment operator
·      Increment and decrement operator
·      Bitwise operators
·      Bitwise assignment operators
·      Relational operators
·      Boolean logical operators
·      Ternary operators
Basic arithmetic operator
    +         Addition
    -         Subtraction
    *         Multiplication
    %         Modulus
    /         Division
Arithmetic Assignment Operator
+=,-=,*=,%=,/=
Increment and decrement Operator
Types of Increment
Post increment (a++)
Pre increment (++a)
Types of Decrement
Post Decrement(a--)
Pre Decrement (--a)
Example
Post increment
int a=10;
int b;
b=a++; -> b=a, a=a+1
Answer  b=10,a=11

Pre increment
b=++a; -> a=a+1,b=a
Answer  b=11,a=11
Post decrement
int a=10;
int b;
b=a--; -> b=a, a=a-1
Answer  b=10,a=9
Pre decrement
b=--a; -> a=a-1,b=a
Answer  b=9,a=9
Bitwise Operator
   &               Bitwise AND
   |               Bitwise OR
   ~               Bitwise NOT
   ^               Bitwise XOR
   <<              Left shift operator
   >>              Right shift operator
   <<<             Left shift with zero fill
   >>>             Right shift with zero fill
Bitwise Assignment Operator
   &=              Bitwise and assignment operator
   |=              Bitwise or assignment operator
   <<=             Bitwise left shift assignment operator
   >>=             Bitwise right shift assignment operator
   >>>=            Bitwise right shift with zero fill assignment
                   operator
Relational operators
     ==           Equal to
     !=           Not equal to
     >            Greater than
     <            Less than
     >=           Greater than or equal to
     <=           Less than or equal to
Boolean Logical operators
     &            Logical and
     |            Logical or
     ^            Logical XOR (exclusive or)
     ||           Short circuit or
     &&           Short circuit and
     !            Logical unary not
     &=           And assignment
     |=           Or assignment
     ^=           XOR assignment
Ternary operator
     ?:           Ternary if..then..else
Operator precedence
In mixed operators expression, java follows following precedence
     ()       []
     ++       --
     *        /      %
     +        -
     >>       >>>    <<
     >        >=     <      <=
     ==       !=
     &
     ^
     |
     &&
     ||
     ?:
     =        op=
Note
Operator precedence can be overcome by using parenthesis. Consider following example
int x=5+2*3 // Ans: 11 because * first, + second
It can be rewritten as
int x=(5+2)*3; // Ans 21
Above example illustrates,  by using parenthesis, we can change the operator precedence.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.