Page 61 - iPlus_Ver_2.0_class_8
P. 61

For example:

                        int x = y + z;
                        or
                        int x = 10 + 20;

                 In the above example, the plus (+) operator is used to calculate the sum of two variables (y and z)
                 or values (10 and 20). It calculates the sum and assigns the result to the variable x.
                 There are various types of operators in Java, which are as follows:

                 •  Arithmetic operators
                 •  Relational operators

                 •  Logical operators
                 •  Unary operators

                 •  Assignment operators
                 Let's discuss these in detail.
                 Arithmetic Operators


                 Arithmetic operators are used to do basic mathematical calculations. These are known as binary
                 operators, which means they require two operands to perform calculations. These are as follows:
                                                                                           Example
                    Operator                    Description                  Syntax                        Output
                                                                                       (int a = 11, b = 4)
                        +         Returns the result after performing the     a + b          a + b            15
                    addition      addition operation.
                        –         Returns the result after performing the     a – b          a – b             7
                   subtraction    subtraction operation.

                        *         Returns the result after performing the      a * b          a * b            44
                  multiplication multiplication operation.

                        /         Returns the result after performing the      a / b          a / b            2
                     division     division operation.

                        %         Returns the remainder after performing      a % b          a % b             3
                   remainder      the division operation.


                 Relational Operators

                 Relational operators help to compare quantities and return the Boolean value ‘True’ or ‘False’ as
                 a result. These are also known as binary operators.

                                                                                           Example
                    Operator                  Description                  Syntax                          Output
                                                                                      (int a = 11, b = 4)
                       ==        This is used  to  check  the  equality  (a == b)     a == b              false
                    Equal to     between two values.                                  a == 11             true




                                                                                                                  59
                                                                                               Program Coding
   56   57   58   59   60   61   62   63   64   65   66