Why Operators...?
Using this we can write logic for client requirements into methods. Operators in java -
1. Arithmetical Operators - Using this we can write logic for arithmetical operations. +, -, *, /, %
2. Relational Operators - Using this we can write logic for single comparison of task. >, <, >=, <=, ==
3. Logical Operators - We can write the logic for multi-comparison of tasks. &&(and), ||(or), !(not)
4. Unary Operators - Using this we can update the logic. Increment (++), Decrement (--)
- Pre-Increment (++a)
- Post-Increment (a++)
- Pre-Decrement (--a)
- Post-Decrement (a--)
Example - int a=11, b=22, c=6, res;
res = (a++ + b++) + (b++ - c++) + ((++a + ++b)++c)
= (11+22)+(23-6)+(13+25)8
=33+17+(38)8
=50+304
=354
Pre means direct evaluation and direct updation. Post means direct evaluate but it is not updating. This updation is from next or near same variable. When we are using updation, operators by default it will be update one value.
5. Ternary Operator - Using this we can write single comparison of tasks without control statements. ?:
Example - int a=10,b=5,c;
c = a>b ?a:b;
6. Assignment Operator - Using this we can assign the requirement to the variable. =
Example - int a=10;
7. Binary Operator - Using this we can write logic for binary operations. &(single and), |(single or) , <<(left shift), >>(right shift)
Example : int a=55,b=25;
a=110111, b=011001
a&b = 010001
a|b = 111111
a<<b = 110010
a>>b = 000100
No comments:
Post a Comment