Page 159 - Computer science 868 Class 12
P. 159

For example:  if(a==5)
                               {
                                   System.out.println("a is equal to 5");
                                   System.out.println("The code ends");
                              }
                 2.  Brackets ‘[]’: The opening and closing brackets are used as array element references.
                   For example: a[], arr[10], etc.
                 3.  Parentheses ‘()’: They are used to represent the methods or functions. They are also used to enclose any arithmetical,
                   logical or relational expression to set the priority of the evaluation.
                   For example: main(), sum(), etc.

                     6.5 OPERATORS
                 Operators are the special symbols that are used to instruct the compiler to execute some operations or calculations.
                 They are used to perform mathematical or non-mathematical operations in a program.

                 There are two parts in an expression, i.e., operands and operators. The variables or values that are used in an expression
                 are called operands and the symbols on which the operand works such as + (addition), - (subtraction), etc. are known
                 as operators.

                 Java usually has a group of built-in operators with some specific meaning assigned to them, though in some special
                 cases, it allows programmers to assign different meanings. Consider the following example code.

                 Thus,
                                               double a = 5.6, b = 6.7, c = 4.3, d;
                                               d = a * b - c;
                                               System.out.println("Answer is: " +d);

                 Here, each statement has different types of operators and related operands.
                 •  In the 1st line:  = acts as an assignment operator and a, b, c, d, 5.6, 6.7 and 4.3 are operands.
                 •  In the 2nd line: = acts as an assignment operator; *, - are arithmetical operators, and a, b, c and d are operands.
                 •  In the 3rd line: + acts as a concatenation operator and not the arithmetical operator; and d is an operand.
                 There are different types of operators in Java which are as follows:
                 •  Arithmetic operators

                 •  Relational operators
                 •  Logical operators
                 •  Bitwise operators
                 •  Shift operators
                 •  Assignment operator

                 6.5.1 Arithmetic Operators
                 Arithmetic operators are used to perform different arithmetical calculations as and whenever needed by the program.
                 There are different types of arithmetic operators. They are addition (+), subtraction (-), division (/), multiplication (*)
                 and modulus (%).
                 1. Addition (+) Operator: It is used for adding two values either of integer literal or real literal. The result may be
                   positive or negative depending on the values.
                   For example:  if int a=10, b=15;
                                a+b = 10+15 = 25



                                                                                                                       157
                                                                                               Variables and Expressions   157
   154   155   156   157   158   159   160   161   162   163   164