Page 335 - Computer_Science_F5
P. 335

Computer Science           Program Example 5.3:

            Java program to demonstrate static variable

              public class LibraryBooks {
          FOR ONLINE READING ONLY
               // Static variable declared using the static keyword
                  static int ChkBookValue = 30;
                  public void getTitle() {
                      // Accessing the static variable within the method
                      System.out.println(“Number of books in shelves: “ + ChkBookValue);
                  }
               public static void main(String[] args) {
                      // Calling the static variable directly without creating an object
              System.out.println(“Number  of books in shelves (direct access): “ + LibraryBooks.
              ChkBookValue);
                      // Creating an object of the class to access the method
                      LibraryBooks obj = new LibraryBooks();
                      // Calling the method which uses the static variable
                      obj.getTitle();
                  }
              }




           Operators
           In Java, an operator refers to a symbol utilised for executing specific actions or
           operations. Java offers a diverse array of operators designed to manipulate variables.
           These include symbols such as +, -, *, and /, among others. Java operators can be
           categorised into several groups, including:

           (a) Arithmetic operators
           This include Multiplicative: *, /, % and Additive: +, -. The arithmetic operators
           function similarly to those in algebra, allowing for various arithmetic operations.
           The Table 5.7 below illustrates these operators. For example, when integer A = 5,
           B = 15, the output is shown.

           Table 5.7: Arithmetic operators

            Operator              Description                    Example    Output
            +(Addition)           Adds values A &B               A+B        20
            -(Subtraction)        Subtracts B from  A                                                                                                                             A-B  -10



                                                 326
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   326                                                     23/07/2024   12:34
   330   331   332   333   334   335   336   337   338   339   340