Method Parameters & Arguments In Java

In our previous we introduced Java methods. Now in this tutorial we will discuss how you can pass parameters into a method, return values and then we discuss method with if … else. We can have certain information passed into method as parameter.

What is a parameter?

Parameter -this is simply a value that you can pass to a method in Java. Parameter acts as variables within the method. To specify the parameter you do it after the method name in between the parentheses. Whereby you can add as many parameters as you wish then you separate them with a comma(,). For instance, firstName, age, myNum these are parameters.

What is an Argument?

Argument -refers to the actual values that are passed to variables defined in the method header when the method is called from another method. That is, whenever any particular method is called during the execution of the program, there are some values that are passed to call that particular method. So we can say when a parameter is passed to the method, it is called an argument. For instance, john, 23 these are arguments.

Example


public class Demo {
 Static void myMethod(String school){
 System.out.println(school + " university");
}

public static void main(String[] args){ myMethod("Maseno"); myMethod("Egerton"); myMethod("Kibabii"); myMethod("Machakos"); } }

It will output; Maseno university Egerton university Kibabii university Machakos university

In the above example, we have school as the parameter while maseno, egerton, kibabii, machakos as the arguments.

Passing multiple parameters

You can as well pass several parameters into a method. That is, you can have string, int, float or any other data type passed at once into a method. What you need to know is that the number of parameters and arguments should be the same and in the same order. Look at this example below;


public class Demo {
 Static void myMethod(String middleName, int age){
 System.out.println(middleName + " is " + age + " years old");
}

public static void main(String[] args){ myMethod("Philloh", 19); myMethod("John", 27); myMethod("Alex", 30); myMethod("Stephen", 23); } }

It will output; Philloh is 19 years old John is 27 years old Alex is 30 years old Stephen is 23 years old

Methods with return values

As we discussed in our previous tutorial we said that we use void keyword to show that the method does not have return value. So to create methods with return values you can simply use primitive data types in Java instead of using void. return keyword is used inside the method, some of the data types are int, str, char, float etc. Methods with return values can be used to perform common mathematical calculations. Look at this example where you add two values;


public class Demo{
 static int myMethod(int y, int z){
 return y + z;
}

public static void main(String[] args){ System.out.println(myMethod(100, 50)); } }

It will output; 150

Methods with condition(if … else)

You can simply have methods/functions where you give them a certain condition if the condition is true then it output certain statements, alternatively when the condition is false there is certain information to be printed. Check at this example below;


public class Demo{
 static void myMethod(int age){
//It checks whether condition is true or false
if(age > 18) {
 System.out.println("Qualified for an ID card");
] else {
 System.out.println("Does not qualify for an ID card");
}
}
public static void main(String[] args){
myMethod(34); //Here you call myMethod method and pass an age of 34
 }
}


It will output;Qualified for an ID card
Since the age of 34 is greater than 18.
Hope now you understand how to pass arguments and parameters into a method. You may watch our videos on our YouTube Channel to learn more through presentations and live sessions we regularly holds. Let’s meet in our next tutorial for more awesome content in Java. Thank you!