Monday, 16 November 2009

More on Methods

As we discussed earlier , methods contain code to do some process or task
General form of a method is :
return type name (parameter-list)
{
// code to do some process ( body of method)
}
return type : A method can return some value like a number or String or any valid data type including class types
name : name is any valid identifier
parameter-list : it is the list of type-identifier pairs seperated by , . These identifiers receive the value of arguments passed in a method call

  • In Java , every method must have a return type and it must return a value compatible with it's return type
  • If a method does not return a value , it's return type must be void

    example 1
    int getNumber ()
    {
    int number = 10 ;
    return number ;
    }
    example 2
    String getNumber ()
    {
    int number = 10 ;
    return number ;
    }
    method in example 2 would cause compile time error , as returned value and return type are not compatible
    example 3
    void call (int input1 , int input2)
    {
    // code
    }
    call (5 , 10) ; // ok input1 will get value 5 and input2 will get value 10
    call (5) ; // error as passed values (arguments) do not match the number of parameters
    call (5 , "abc") ; // error as second argument is String
  • Number , type and sequence of passed values must match that in the parameter list
  • Passed value can be a variable like x,y or it can be a literal value like 5, 10 , "abc"

    Method overloading

    In a class , you can not have two methods with same name and signature
    link: Method Signature It consists of name of method and it's parameter list
    example
    void method1 (int a , int b)
    {
    // code
    }
    You cannot have another method with same name and same parameter list in same class
    But you can have same method name but different signature. This is called method overloading

Signature can be changed by changing

  • the number of parameters
  • same number but changing the types
    Lets see how overloading works and how it helps
    void method1 (int a , int b) // two int parameters
    void method1 (int a , float b) // two parameters: one int and another float
    void method1 (int a , int b , String c) // different number of parameters
    void method1 (int c , int d) // invalid : signature same as first one


First three forms are overloaded as they have different signatures
Fourth form is invalid , because for method call
method1 (5,10) ;
It is not possible to determine which
version to call , first or fourth

  • When a method call is there , Java looks for a match between the signature in method call
    and the method definition.
  • It must match to one method whose signature matches the method call exactly

    Advantage of Overloading
    It allows us to have multiple methods having similar processing with the same name ,
    which can be easier to handle then having method names like method1 , method2 ,
    method3 , method4 , method5 and so on

    Argument passing
    We now know , that we can pass variables or literals in a method call
    which are recieved by the corresponding parameters in method
    definition
    There are two ways of argument passing
    By value
    : for primitive data types like int , float , boolean
    By reference : for objects

example ( By value )
void one ( int a , int b)
{
a = a + 5 ;
b = b + 5 ;
System.out.println ( "Function one " ) ;
System.out.println ( "a = " + a + " b = " + b ) ;
}
void two ( )
{
int a = 5 ;
int b = 10 ;
one ( a , b) ;
System.out.println ( "Function two " ) ;
System.out.println ( "a = " + a + " b = " + b ) ;
}
Output :
Function one
a = 10 b = 15
Function two
a = 5 b = 10

  • The value of a and b in function two does not get changed.
  • Reason being value of these variables was copied into
    parameters of function one.
  • There is no relation after this between passed arguments
    and in the recieving parameters

    example ( By reference )
    Using the Student class defined earlier
    void one ( Student input)
    {
    input.name = "James" ;
    input.age = 30 ;
    System.out.println ( "Function one " ) ;
    System.out.println ("Name : " + input.name + " , age " + input.age ) ;
    }
    void two ( )
    {
    Student newStudent = new Student () ;
    newStudent.name = "Harry" ;
    newStudent.age = 20 ;
    one ( newStudent ) ; // pass this object as argument
    System.out.println ( "Function two " ) ;
    System.out.println ("Name : " + newStudent.name + " , age " + newStudent.age ) ;
    }
    Output:
    Function one
    Name : James , age 30
    Function two
    Name : James , age 30
  • Memory address stored in newStudent is copied into parameter input
  • Any changes we make on input object , they will reflect on newStudent object
  • Object is same , only it is being referred by two different variables

    To summarize
  • Primitive types ( byte , int etc.) are passed by value and Objects are passed by reference
  • In by value , value of argument is copied into the parameter. Any changes to the receiving
    parameter won't affect the original value
  • In by reference , memory address of of argument is copied into the parameter. Any changes to the receiving parameter would affect the original value as it refers to the same Object (same memory location)