Page 157 - ComputerScience_Class_11
P. 157

7.2 NAMED CONSTANTS (FINAL)
                 While variables can hold values that change during the execution of a program, there are situations where you might
                 need to store a value that should remain constant throughout the program. This can be achieved by using the final
                 keyword in Java. A named constant is a variable that is declared with the final keyword, ensuring that its value cannot
                 be changed after it is initialized.
                 To declare a named constant, you use the final keyword along with a descriptive name and it is typically written in
                 uppercase letters to distinguish it from regular variables. This helps improve the readability of the code and ensures
                 that the constant’s value remains unchanged.
                 Let us take an example.

                  Program 1      Write a program to demonstrate the use of the final keyword.

                   1      class final_keyword
                   2      {

                   3          final int marks = 100;
                   4          void change()
                   5          {
                   6              //The compiler will report an error because the value
                   7              // of a final variable cannot be changed.
                   8              marks = 200;
                   9          }

                  10
                  11          public static void main(String args[])
                  12          {
                  13              final_keyword obj = new final_keyword();
                  14              obj.change();
                  15          }
                  16      }

                 When we try to change the value of the final variable in the given program, an error message is shown as “cannot
                 assign a value to final variable marks” and the program will not be executed.



                     7.3 OPERATORS
                 In the previous chapter, we have seen different types of operators. These operators are used for doing calculations. In
                 Java, there are mainly three categories of operators which are as follows:

                 •  Unary operator: A unary operator works only on one operand.
                 •  Binary operator: As the name suggests, a binary operator works on two operands.
                 •  Ternary operator: A ternary operator works on three operands.
                 While using operators, we must first understand what the operands are. The operands are the objects on which an
                 operation is performed. In a programming language, an operand can be a variable or a constant.

                 Suppose,

                                                                 a + b = 5;





                                                                                            Variables and Expressions  155
   152   153   154   155   156   157   158   159   160   161   162