Chapter5 Methods
Chapter5 Methods
A method is a set of Java statements, which is referred to by a name and can be called
at any point in an application simply by its name. When method name is encountered in
an application, execution of the program branches to the body of the method. After the
method is executed, the control returns to the area of the main program code from
which it was called, and the program continues at the next statement. Java uses stack
to remember the address of Java statement from where the control is transferred to the
method, so that it can return to the calling place.
DEFINING A METHOD
access-type return-type method-name(parameters)
{
// body;
}
public static int max(int a, int b)
{
int big = a > b ? a : b;
return big;
}
Here, the access-type is public static, return type is int, name of the method is max,
there are two arguments a & b and the method returns an int value
CALLING A METHOD
variable = method-name(arguments);
// Big2.java
import java.util.*;
public class Big2
{
// define method max2(). It receives two integers and returns the biggest as an
//integer. Here variables a, b and result are visible only inside max2() and not
//outside
public static int max2(int a, int b)
{
int result = a > b ? a : b;
return result;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// read two integers
System.out.println(“Enter two integers one by one”);
int a = sc.nextInt();
int b = sc.nextInt();
// now call method with a and b and collect result
int result = max2(a, b);
// show result
System.out.println(“Biggest: ” + result);
}
}
Output
Enter two integers one by one
23
31
Biggest: 31
METHOD OVERLOADING
In Java, it is possible to define two or more methods with the same name, within the
same class. Hence, these methods are called overloaded methods and this process is
known as method overloading. Method overloading is one of Java’s most exciting and
useful features that avoid redundant names for those methods whose behaviour is the
same.
Type of arguments
Number of arguments
Order of appearance of the arguments
Output
30
30.0
300.0
50.0
70.0
JDK 1.5 introduced variable arguments (or varargs) and a new syntax "Type..."
data-type… argument
Varargs can be used only for the last argument and only one variable length argument
can be specified in a method. Any regular arguments must precede it. The three dots
(...) indicate that the last argument may be passed as an array or as a sequence of
comma-separated arguments. The compiler automatically packs the varargs into an
array. You could then retrieve and process each of these arguments inside the method's
body as an array. It is possible to pass varargs as an array, because Java maintains the
length of the array in an associated variable length.
// ShowNames.java
public class ShowNames
{
public static void show(int id, String… names)
{
System.out.println(“id: ” + id);
for(int i = 0; i < names.length; i++)
System.out.println(“Name ” + i + “ : ” + names[i]);
}
public static void main(String[] args)
{
// call show() with varargs - just some value 100
show(100, “C”, “C++”, “Java”, “.NET”, “PhP”, “Python”);
Output
id: 100
Name 0 : C
Name 1 : C++
Name 2 : Java
Name 3 : .NET
Name 4 : PhP
Name 5 : Python
id: 100
Name 0 : C
Name 1 : C++
Name 2 : Java
Name 3 : .NET
Name 4 : PhP
Name 5 : Python
RECURSION
int fact(int n)
{
if (n == 0)
return 1;
return n * fact(n-1);
}