Page 143 - Cs_withBlue_J_C11_Flipbook
P. 143

07                              VARIABLES AND



                                                                              EXPRESSIONS















                          Learning Objectives



                     7.1  Final Variable                               7.2  Operators
                     7.3  Precedence of Operators                      7.4  Associativity of Operators
                     7.5  Arithmetical Expression and Statement



                 We have already learnt in the previous chapter that a variable is a name given to a space in computer memory to hold
                 a value. It is a combination of two words “vary” and “able” meaning changeable that signifies that the values stored in
                 the variable can be changed. It may take different values at different points of execution which depends on the logic
                 of the program.
                 Example:

                    int var = 0;      //Here “var” is a variable of integer type, initialised with the value 0.

                    var = 5;          //Here it is assigned a different value 5.
                 Hence, a variable can take different values at different points of execution.

                 While naming a variable, it should follow certain protocols. Some of them are as follows:
                 •  It must start with a letter, an underscore (_) or a dollar ($) sign.
                 •  Apart from the first character, it can have any combination of characters including alphabets, digits and underscore
                   only.
                 •  A variable name is case-sensitive, i.e., we can declare two different variables with different cases, e.g., int Sum,
                   sum;
                 •  It can be of any length.
                 •  It cannot contain white space.
                 •  Reserved words cannot be used as variables in Java.


                     7.1 FINAL VARIABLE
                 Though the values contained in variables can be changed during the execution of the program, they can be made fixed
                 only. This is done by the keyword “final”. When the word final is used, the value in the variable must be assigned and
                 it will be fixed in the whole program.






                                                                                                                       141
                                                                                               Variables and Expressions  141
   138   139   140   141   142   143   144   145   146   147   148