Methods are sets of java statements which have a specific meaning. Methods are also known as functions. By using functions a complex program is divided into sets of well defined modules.
By using methods in a complex java program, the program become reliable and understandable.
In java the syntax for defining a method is given bellow.
Syntax:
access_specifier return_type method_name(Formal Arguments)
{
local variable deceleration;
statements;
}
Exmple:
public String getMessage(String msg)
{
String message;
message = msg;
return message;
}
The access specifier may be public, private or protected.
The variables declaring inside a method have scope only within the body of that method.
It is recommended that the method name in java started with small letters and if the method name contains more than one words, then separates them with capital letters.
A function in java have two parts.
1. Function definition and
2. Function calling.
The function definition defines the actual body of the function.
Syntax:
access_specifier return_type method_name(Formal Arguments)
{
....................
function body;
....................
}
The function calling is the statement that invokes a function definition.
Syntax:
function_name (actual_ arguments);
Methods are categarized as follows
1. Method with return type and arguments
2. Method with return type and no arguments.
3. Method with no return type and arguments
4. Methods without rerun type and arguments.
1. Method with return type and arguments:
Here the method have both return type and arguments. Consider the following example for more details
Example:
public int findSum(int x, int y)
public int findSum(int x, int y)
{
int a = x;
int b = y;
int c = x + y;
return c;
}
Here x and y are the actual arguments and c is the return variable and all are of integer type.
2. Method with return type and no arguments
In this type of methods only the arguments are present and the actual arguments positions are left blank.
Example:
public int findSum()
public int findSum()
{
int a = 10;
int b = 5;
c = a+b;
return c;
}
Here you can see that the actual argument fields are left blank.
3. Method with no return type and arguments:
In this type of methods the return type of the methods is of "void" type. Here the actual arguments are present.
Example:
Example:
public void findSum(int x, int y)
{
int a = x;
int b = y;
int c = x + y;
System.out.println("Sum = "+c);
}
In the above example you can see that the result after calculating sum is printed within the method itself. That means this type of methods will not return any values to the calling methods.
4. Methods without return type and arguments.
Like the name suggests this type of methods never contains any arguments and return type.
Example:
public void findSum( )
public void findSum( )
{
int a = 5;
int b = 7;
int c = x + y;
System.out.println("Sum = "+c);
}
In the above example you can see that the calling method will not pass any value to this function and the called function will not return any values to the called function.
Comments
Post a Comment