Method in java.

method is a block of code which only runs when it is called. You can pass data, known as parameters, into a methodMethods are used to perform certain actions, and they are also known as functions. Because of method we can not write same program many time.

   In bellow program we are learning about method and understand how method is call in java

package com.ritesh;

public class method2 {
public static int sum(int x,int y)
{
x = 10;
y=22;
int z= x +y;
System.out.printf("The sum of %d and %d is %d ", x,y,z);
return z;
}

public static void main(String[] args) {

sum(22,20);
sum(55,44);

}
}

This is the output we obtained when we run this program.


The sum of 10 and 22 is 32     The sum of 10 and 22 is 32 




Comments