Java Methods with Arguments

What is a method?

A method is a block of code that does a job.

A method can:

What are arguments?

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:

  • Method = a machine
  • Arguments = the input you put into the machine
  • Return value = the output you get back

Why use arguments?

Arguments make methods more flexible.

Instead of writing many similar methods, you can write one method and send in different values.

Example without arguments

public static void sayHello() {
    System.out.println("Hello, Joe");
}

This only works for one name.

Example with arguments

public static void sayHello(String name) {
    System.out.println("Hello, " + name);
}

Now you can use different names:

sayHello("Joe");
sayHello("Maria");
sayHello("Alex");

Basic method format with arguments

public static void methodName(dataType parameterName) {
    // code
}

Example

public static void printAge(int age) {
    System.out.println("Age: " + age);
}

Call it like this:

printAge(16);

Vocabulary

Parameter

A parameter is the variable listed in the method header.

public static void printAge(int age)

Here, int age is the parameter.

Argument

An argument is the actual value passed into the method.

printAge(16);

Here, 16 is the argument.

Method with one argument

public static void doubleNumber(int num) {
    System.out.println(num * 2);
}

Call:

doubleNumber(5);
Output:
10

Method with two arguments

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);
Output:
7

Method with different data types

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);
Output:
Jordan is in grade 10

Order matters

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");
This causes an error because the types are in the wrong order.

Methods with return values and arguments

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);
Output:
20

void methods vs return methods

void method

A void method does a task but does not return a value.

public static void greet(String name) {
    System.out.println("Hello " + name);
}

return method

A return method sends a value back.

public static int square(int num) {
    return num * num;
}

Example program

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;
    }
}

What happens step by step?

For this call:

printSum(8, 2);

Java does this:

  1. Finds the method printSum
  2. Sends 8 into parameter a
  3. Sends 2 into parameter b
  4. Runs the code inside the method
  5. Prints the result

Common mistakes

1. Wrong number of arguments

addNumbers(5);

This is wrong if the method needs two arguments.

2. Wrong data type

printAge("sixteen");

This is wrong if the method expects an int.

3. Wrong order

showInfo(15, "Emma");

Wrong because the method expects String first, then int.

4. Forgetting to use the return value

multiply(3, 4);

This runs, but if you do not store or print the result, you will not see it.

Real-life analogy

Imagine a vending machine:

Different button choices give different results, just like different arguments.

Quick examples

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)

Summary

A Java method with arguments:

  • receives information when it is called
  • uses parameters to store that information
  • can do a task or return a value
  • must be called with the correct type, order, and number of arguments

Practice questions

  1. What is the difference between a parameter and an argument?
  2. Why are arguments useful in methods?
  3. What is wrong with this call?
printAge("15");
  1. Write a method that takes a student name and prints a welcome message.
  2. Write a method that takes two integers and returns their sum.