Page 59 - CA_Blue( J )_Class10
P. 59

You will get the following output:
















                 Explanation of the output:
                 •  a++: value of a is printed then increased by 1. So, 5 is printed and then the value of a becomes 6.
                 •  --b: value of b is decreased by 1 and then printed. So, the value of b is 5.
                 •  a + b: Binary + operator adds the values of a and b and then assigned to the sum variable. Hence, 6 + 5 = 11
                 •  a - b: Binary - operator subtracts the value of b from a and then assigned to the diff variable. Hence, 6 - 5 = 1
                 •  (a > b)? a : b: Ternary operator will check the condition and return the first expression if the condition becomes true
                   otherwise returns the second expression. Hence, 6 > 5 ? 6 : 5 = 6


                     4.3 TYPES OF OPERATORS
                 Java language provides different sets of operators to do a variety of operations. The different types of operators in Java
                 are:

                 •  Arithmetic operators
                 •  Relational operators
                 •  Logical operators
                 •  Assignment operators
                 •  Ternary operator
                 •  Miscellaneous operators

                 4.3.1 Arithmetic Operators
                 Arithmetic operators are used to perform simple and advanced mathematical operations on the operands which are
                 of primitive data types. Java arithmetic operators are divided into two types: binary and unary.
                                                                              Example
                                Operator           Name of Operator                               Output
                                                                          a = 20 and b = 4
                           +                   Addition                 a + b               24
                           -                   Subtraction              a - b               16
                           *                   Multiplication           a * b               80
                           /                   Division                 a / b               5
                           %                   Modulus                  a % b               0
                           +                   Unary plus               +a                  20
                           -                   Unary minus              -a                  -20
                           ++                  Unary increment          a++                 21
                           --                  Unary decrement          a--                 19

                 Let us study about them in detail.






                                                                                                                       57
                                                                                                      Operators in Java   57
   54   55   56   57   58   59   60   61   62   63   64