Page 228 - Computer science 868 Class 12
P. 228

Let us take the following example for better understanding of the concept.
                  public void equal(int i, int j)
                  {
                      if(i == j)
                         System.out.println("Equal");
                      else
                          System.out.println("Not Equal");
                  }
              Explanation:

              Here, “greater” is the name of the method. The keyword “void” represents that the method does not return any value.
              The keyword “public” means the method can be accessed from anywhere and the body of the method prints whether
              i is equal to j or not.
                                                             Definition

                    Methods are the block of statements in a program which performs a specific task and can be reused as and whenever
                    required. They are also called Functions. Thus, to perform a certain task or operation, a group of statements written
                    together separately defined under a name method is required. The advantage of the method is that we can reuse
                    the code as and when required. This reduces the size of the program, makes the modification easy, and increases
                    the readability of code.


              A method can be of two types which are as follows:
              •  Built-in method
              •  User-defined method


                  8.1 BUILT-IN METHOD
              The build-in methods are already declared in the Java language and they are called and executed as per the requirement.
              These methods are stored under packages. To use them, we have to include the respective package in which they are
              stored. Methods in Java allow us to reuse the code without retyping it.

              These are also known as predefined methods. They are ready to use and can be executed directly anywhere in the
              program. There are many types of built-in methods. Some of the built-in methods are as follows:
              •  Mathematical methods
              •  String methods
              •  Character methods

              Let us understand with the help of an example.
                  class method
                  {
                      public static void main()
                      {
                          int a=5;
                          double d = Math.pow(a,2);
                          System.out.println("Value is "+d);
                      }
                  }
              In the preceding program, three predefined methods are used. They are main(), pow() and println(). These methods are
              directly used as there is no need to declare these of the methods. The println() method is a method of the PrintStream
                                                                                                                  2
              class that prints the result on the monitor screen. The pow() method is a method of the Math class that returns a  and
              the result is in double data type in this code.


                226226  Touchpad Computer Science-XII
   223   224   225   226   227   228   229   230   231   232   233