2 Aug 2012

Methods in java

A method is a collection of statements that are grouped together to perform an operation. 
       Creating a Method:
           In general, a method has the following syntax:

           modifier return_type method_name(list of parameters)
          {
              //method body;
          }

       A method definition consists of a method header and a method body. Here are all the parts of          a method:
  • Modifiers: The modifier, which is optional, tells the compiler how to call the method. This defines the access type of the method.
  • Return Type: A method may return a value. The return_type is the data type of the value the method returns. Some methods perform the desired operations without returning a value.
  • Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature.signature represents any one of the following.
    -> Number of Parameters
    -> types of Parameters
    -> Order of Parameters, at least one thing must be different
  • Parameters: A parameter is a container. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.
  • Method Body: The method body contains a collection of statements that define what the method does.

    Calling a Method:

    In creating a method, you give a definition of what the method is to do. To use a method, you have to call or invoke it. There are two ways to call a method; the choice is based on whether the method returns a value or not.
    When a program calls a method, program control is transferred to the called method. A called method returns control to the caller when its return statement is executed or when its method-ending closing brace is reached.
    If the method returns a value, a call to the method is usually treated as a value. For example:
    int l=max(10,20);

    println() method, print()  method are two predefined methods present in a predefined class called PrintStream class. To access these two methods we must create an object of PrintStream class. an object of PrintStream class called out created as static data member in another predefined class called System. Hence, println() and print()  methods must be accessed as follows
    System.out.print();
    System.out.println();

    Using Command-Line Arguments:
    Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command-line arguments to main( ).
    A command-line argument is the information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy.they are stored as strings in the String array passed to main( ).
    Example:
    The following program displays all of the command-line arguments that it is called with:

    class demo
    {
      public static void main(String arg[])
      {
        for(int i=0;i<arg.length;i++)
        System.out.println(arg[i]);
      }
    }

    Try executing this program, as shown here:
    C:\java demo this is command line args
    Output:
    this
    is
    command 
    line 
    args

    Overloading Methods: Overloaded Method is one in which method name is same but its signature is different.
    ex:
    public void test(double  a, double b)
    {
                  //method body;
    }

    public void test(int a, int b)
    {
                  //method body;
    }

    If you call test with int parameters, the test method that expects int parameters will be invoked; if you call test with double parameters, the test method that expects double parameters will be invoked. This is referred to as method overloading.
    Overloaded methods must have different parameter lists. You cannot overload methods based on different modifiers or return types.
    Constructors:
    Constructors are special methods which will be called by the JVM automatically whenever an object is creating for placing our own values. Most often you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method.

    Advantages:
    1. It eliminates in placing default values.
    2. It eliminates in calling ordinary methods explicitly.
    3. Constructors are always used for initializing an object.

    ex:

    class myclass
    {
      int x;
      myclass()
      {
         x=10;
       }
    }

    class demo
    {
     public static void main(string arg[])
     {
       myclass my=new myclass(); //You would call constructor to initialize objects
      }
    }

    Passing Parameters by Values:

    When calling a method, you need to provide arguments, which must be given in the same order as their respective parameters in the method specification.
    When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as pass-by-value. If the argument is a variable rather than a literal value, the value of the variable is passed to the parameter. The variable is not affected, regardless of the changes made to the parameter inside the method.

    ex:
    public static void Println(String msg, int n)
    {
       for(int i=0; i<n; i++)
       System.out.println(msg);
    }

    Here, you can use Println("Hello", 3) to print "Hello" three times. The Println("Hello", 3) statement passes the actual string parameter, "Hello", to the parameter, msg; passes 3 to n; and prints "Hello" three times. However, the statement Println(3, "Hello") would be wrong.

0 comments:

Post a Comment