A method is a block of code that does a job.
A method can:
Arguments are values you send into a method when you call it.
They give the method information it needs to do its work.
Think of it like this:
Arguments make methods more flexible.
Instead of writing many similar methods, you can write one method and send in different values.
public static void sayHello() {
System.out.println("Hello, Joe");
}
This only works for one name.
public static void sayHello(String name) {
System.out.println("Hello, " + name);
}
Now you can use different names:
sayHello("Joe");
sayHello("Maria");
sayHello("Alex");
public static void methodName(dataType parameterName) {
// code
}
public static void printAge(int age) {
System.out.println("Age: " + age);
}
Call it like this:
printAge(16);
A parameter is the variable listed in the method header.
public static void printAge(int age)
Here, int age is the parameter.
An argument is the actual value passed into the method.
printAge(16);
Here, 16 is the argument.
public static void doubleNumber(int num) {
System.out.println(num * 2);
}
Call:
doubleNumber(5);
10
A method can have more than one parameter.
public static void addNumbers(int a, int b) {
System.out.println(a + b);
}
Call:
addNumbers(3, 4);
7
Parameters can be different types.
public static void studentInfo(String name, int grade) {
System.out.println(name + " is in grade " + grade);
}
Call:
studentInfo("Jordan", 10);
Jordan is in grade 10
When calling a method, the arguments must match the parameters in:
public static void showInfo(String name, int age) {
System.out.println(name + " is " + age + " years old.");
}
Correct call:
showInfo("Emma", 15);
Wrong call:
showInfo(15, "Emma");
Some methods take arguments and also return a value.
public static int multiply(int x, int y) {
return x * y;
}
Call:
int answer = multiply(4, 5);
System.out.println(answer);
20
A void method does a task but does not return a value.
public static void greet(String name) {
System.out.println("Hello " + name);
}
A return method sends a value back.
public static int square(int num) {
return num * num;
}
public class MethodArgumentsExample {
public static void main(String[] args) {
greetStudent("Ava");
printSum(8, 2);
int result = multiply(3, 4);
System.out.println("Product: " + result);
}
public static void greetStudent(String name) {
System.out.println("Welcome, " + name);
}
public static void printSum(int a, int b) {
System.out.println("Sum: " + (a + b));
}
public static int multiply(int x, int y) {
return x * y;
}
}
For this call:
printSum(8, 2);
Java does this:
printSum8 into parameter a2 into parameter baddNumbers(5);
This is wrong if the method needs two arguments.
printAge("sixteen");
This is wrong if the method expects an int.
showInfo(15, "Emma");
Wrong because the method expects String first, then int.
multiply(3, 4);
This runs, but if you do not store or print the result, you will not see it.
Imagine a vending machine:
Different button choices give different results, just like different arguments.
public static void sayColor(String color)
public static void printScore(int score)
public static int add(int a, int b)
public static double divide(double a, double b)
public static void describePet(String petName, int age)
A Java method with arguments:
printAge("15");