Home >>Java Tutorial >Java Method Parameters

Java Method Parameters

Java Method Parameters

Java Method Parameters are basically the values that are passed to the methods. And a method can have one or two parameters in them.

In simple words it can be said that the information is passed to the methods as parameter. Inside the methods, the Parameters generally act as a variable inside the method.

Parameters are generally specified just after the method name that too inside the parentheses (). Programmers can as much parameter as they want, all they have to do is to just separate them with a comma.

Whenever the method is called the programmer generally passes along a first name that is used inside the method in order to print the full name.

Here is an example that is depicted below that has a method that takes a String called name as a parameter, please have a look at it for better understanding:

public class DemoClass 
{
  static void DemoMethod(String name) 
  {
    System.out.println(name + " Kumar");
  }

  public static void main(String[] args) {
    DemoMethod("Sanjeev");
    DemoMethod("Abhishek");
  
  }
}
Output :
Sanjeev Kumar
Abhishek Kumar

Please note that whenever a parameter is passed to the method then it is known as an argument.

Multiple Parameters

As discussed earlier, the programmers can have as many parameters as they like.

Here is an example of the same that is depicting the multiple numbers of parameters:

public class DemoClass{
  static void DemoMethod(String name, String profile)
  {
    System.out.println("Name : "+name + " Profile : " + profile);
  }

  public static void main(String[] args) {
    myMethod("Sanjeev ","Developer");
    myMethod("Shipra ","Web Designer");
    myMethod("Manish ", "Developer");
  }
}
Output :
Name :Sanjeev Profile : Developer
Name :Shipra Profile : Web Designer
Name :Manish Profile Developer

Please note that whenever a programmer is working with the multiple parameters then the method call must have the same number of arguments as there are number of parameters and he/she must make sure that the arguments must be passed in the exact same order.

Return Values

The purpose of using the void keyword is that it generally indicates that the method should not return a value. In case the programmers want the method to return a value then he/she can use a primitive data type (like int, char, etc.) instead of the void keyword and should use the return keyword inside the method.

Here is an example of the same that is depicted below that will enhance your understanding about the topic:

public class DemoClass
{
  static int DemoMethod(int a) 
  {
    return 10 + a;
  }

  public static void main(String[] args) 
  {
    System.out.println(DemoMethod(5));
  }
}
Output : 15

Here is another example that is depicting the sum of a method's two parameters in the Java language, please have a look:

public class DemoClass 
{
  static int DemoMethod(int a, int b) 
  {
    return a + b;
  }

  public static void main(String[] args) 
  {
    System.out.println(DemoMethod(10,5));
  }
}
Output : 15

Please note that the you can also store the result inside a variable and it is generally recommended, as it is very easy to read and maintain.

Here is an example of the same:

public class DemoClass 
{
  static int DemoMethod(int a, int b) 
  {
    return a + b;
  }

  public static void main(String[] args) 
  {
    int c = DemoMethod(10,5);
    System.out.println(c);
  }
}
Output : 15

Use of If...Else inside a Method

Yes, it is very much usual that you will see the use of if...else statements inside the methods.

Here is an example that is depicting the use of If-Else statement in the methods:

public class DemoClass{
  static void checkEvenOdd(int num)
  {
    if (num >0)
	{
      System.out.println("Given number is even");
    } 
	else 
	{
      System.out.println("Given number is odd");
    }
  }
  public static void main(String[] args) 
  {
    checkEvenOdd(10); 
  }
}

No Sidebar ads