In java method is a collection of statements it is used to perform some operation. Depends on user needs they create own method with argument or without argument and with return type or without return type.
Syntax:
Modifier returntype methodname( argumentlist)
{
Body of the content
}
Example:
Public static void add( int a, int b ) { Int c; c= a+b; }
There are two ways to call the method that is with return type or without return type.
Syntax;
If it have return type then
Datatype variable = methodname(arguments);
If no return type means
Methodname(arguments);
Passing parameters by value:
Example:
In this example using pass by value method will executed.
public class sample { public static void main(String[] args) { int x = 20; int y = 15; int z; // Invoke the method Z=add(x, y); System.out.println("addition value is :" + z); } public static int add(int x, int y) { // add x with y int a; a=x+y; return a; } }
Output
Addition value is : 35
Follow Us