A method holds the blocks of code and execute when it is called .
- It is a block of code that performs a specific task.
- Method are bound to a class and they define the behaviour of a class
Types of Method in Java
- Standard Library Methods
– It is already predefined in Java Library such as println, printf etc. - User- Defined Methods
– User created their own method, using the specific Syntax.
Variation in Java
There are four variation of Method in Java
- Method with no argument and no return type
- Method with return type but no arguments
- Method with argument but no return type
- Method with both argument and return type
Example to demonstrate Methods in Java
package practice_12;
public class Methods
{
//1. Method with no argument and no return type
public static void sum()
{
int a=5, b=5;
int sum = a+b;
System.out.println("sum = "+ sum);
}
//2.Method with return type but no arguments
public static int sub()
{
int a=10, b=5;
int diff=a-b;
return diff;
}
//3.Method with argument but no returntype
public static void mul(int a , int b)
{
System.out.println("mul= "+ a*b);
}
//4.Method with both argument and returntype
public static double div(double a, double b)
{
// double divide= a/b;
return a/b;
}
public static void main(String[] args)
{
sum();
// int subtract=sub();
System.out.println("sub= "+ sub());
mul(5,6);
System.out.println("division = "+ div(10.5,5.5));
mul(6,6);
}
}