java method

Methods/ function

A Java method is a collection of statements that are grouped together to perform an operation. A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name.
When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console.
A method or function used to perform some action , it is a subprogramme that can be called anywhere in the program.
e.g Arithmatic operation performed by using method.
Arithmatic operation are addition, subtraction, division, and multiplication.
suppose that these operation are method or function of arithmatic program.
let suppose we have a calculator and we going to perform some operation on it.such as add, sub, etc.

Programme of method or function
class Calculator{
int a=3;
int b=5;
void addition(){
int sum=a+b;
System.out.println(“sum is : “+sum);
}
void subtraction(){
int sub=a-b;
System.out.println(“subtraction is : “+sub);
}
void multiplication(){
int mul=a*b;
System.out.println(“multiplication is : “+mul);
}
void division(){
float c= (float)b;
float div=11/c;
System.out.println(“division is : “+div);
}
public static void main(String[] args){
Calculator c = new Calculator();
c.addition();
c.subtraction();
c.multiplication();
c.division();
}
}

Output: sum is : 8
subtraction is : -2
multiplication is : 15
division is : 0.6

BACK

NEXT