Page 350 - Computer_Science_F5
P. 350

Category                  Operator                               Associativity
               Bitwise AND               &                                      Left-to-right

               Bitwise XOR               ^                                      Left-to-right
               Bitwise OR                |                                      Left-to-right      Chapter Five: Object oriented programming with Java
          FOR ONLINE READING ONLY
               Logical AND               &&                                     Left-to-right
               Logical OR                ||                                     Left-to-right
               Ternary                   ?:                                     Right-to-left
               Assignment and Lambda     = += -= *= /= %=>>=<<= &= ^=| = => Right-to-left



              Constants in Java                        (i)  Enhancing readability: Employing
              In Java, a constant is a value that cannot     descriptive  names for constants
              be changed once it is assigned. To declare     enhances code comprehension.
              a constant, the “final” keyword is used,   (ii)  Improving maintainability: Altering
              indicating that the variable’s value is        a single constant is simpler than
              unmodifiable. Think of a constant like a       hunting down and replacing
              labeled box where you store something that     scattered values in your codebase.
              cannot be changed unless you relabel it.       It is similar to adjusting a formula

              In Java, it is common practice to write        once rather than revising each set
              constant names in capital letters, with        individually!
              multiple words separated by underscores  (iii)  Potential performance boost: In select
              (_).  Constants  can  also  be  defined  as    scenarios, employing constants may
              static, which allows them to be accessed       yield marginally faster programs
              without creating an instance of the class.     since values needn’t be recalculated
                                                             repeatedly. It’s analogous to pre-
              One important  aspect  of constants  is        measuring ingredients to streamline
              that they can hold any type of data, such      baking.
              as numbers or text.  They are similar
              to  recipes  where  a  fixed  amount,  like  (b)   Declaration of constant variable
              1 teaspoon of sugar, is always used in          in Java
              a standard tea  cup, regardless of the   In Java, there is no specific “constant”
              number of cups you plan to drink (unless   keyword. Instead,  two keywords are
              you want very sweet tea).                used together to declare constants:


              (a) Importance of using constants in     Final:  The  “final”  keyword  ensures
                   Java                                that the value of the constant cannot be
              There are several reasons of using       changed after it is assigned. It is similar
              constant as variables in Java, some of   to following a guideline or rule on how
              the reasons includes:                    something should be done.

                                                    341
               Student’s Book  Form Five



     Computer Science Form 5.indd   341                                                     23/07/2024   12:34
   345   346   347   348   349   350   351   352   353   354   355