Java Method Parameters

Java Method Parameters Tutorial


Define :

  • A method that take value at the time of calling known as parameterised method.
  • The fundamental parts of a method are the name, the arguments, the return type, and the body.
  • In simple words methods with arguments, or variable in side the parentheses are called parameterised methods, and the arguments are called parameters.
  • A method can have one parameter, or more parameters inside the parentheses.

Syntax -

 

ReturnType methodName( /* Argument list */ )
{       
             
/* Method body */
               }

Example1 -

public class T{
 
static void show(int a) {
    System.out.println(a+a);
  }

 
public static void main(String[] args) {
    show(4);
    show(6);
    show(9);
  }
}
-----------------------------
Output -
8

12

18

Example2 - Similarly we can have more then one argument.

 

public class T{
 
static void show(int a, int b) {
    System.out.println(a+b);
  }

 
public static void main(String[] args) {
    show(4,6);
    show(6,2);
    show(9,1);
  }
}
-----------------------------
Output -
10

8

10

Note - we can have any type of argument like -int, String, float, etc.

 

Method with Returning value

  • A method can return a value to caller if method return a value to caller if  method return type is not void.
  • A method can return only one value.
  • Return variable type, and method return type must be same.
  • We can have method with returning values as well we will understand with an example.

Syntax -

 

ReturnType methodName( /* Argument list */ )
{       
             
/* Method body */

                  Returning value;
               }

Example - create a method that take two string as a parameter, and return concatenated string.

Output :