Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Method Parameters

In Methods you wrote methods that take input through parameters and give back a result with return. This topic looks closer at how values travel into and out of a method, because that explains several things that surprise beginners.

Several parameters and a return value

Parameters are separated by commas, and each one has its own type. The arguments in the call must match them in number, order and type:

public class Main {
    static double finalPrice(double price, int quantity, double discountPercent) {
        double total = price * quantity;
        return total - total * discountPercent / 100;
    }

    public static void main(String[] args) {
        System.out.println(finalPrice(250.0, 4, 10));
        System.out.println(finalPrice(99.5, 2, 0));
    }
}
Output
900.0
199.0

Java passes copies of values

When you call a method, each parameter receives a copy of the argument's value. Changing the parameter inside the method does not change the caller's variable:

public class Main {
    static void addTen(int n) {
        n = n + 10;
        System.out.println("Inside: " + n);
    }

    public static void main(String[] args) {
        int score = 5;
        addTen(score);
        System.out.println("Outside: " + score);
    }
}
Output
Inside: 15
Outside: 5

To get a changed value back, return it: score = addTen(score); with a method that returns n + 10.

Arrays and objects: a copy of the reference

An array variable holds a reference to the array, not the elements themselves. The method gets a copy of that reference, which points to the same array. So a method can change the elements, and the caller sees the change:

import java.util.Arrays;

public class Main {
    static void doubleAll(int[] values) {
        for (int i = 0; i < values.length; i++) {
            values[i] *= 2;
        }
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};
        doubleAll(numbers);
        System.out.println(Arrays.toString(numbers));
    }
}
Output
[2, 4, 6]

The same is true for objects, which you will meet in Classes and Objects. Strings are references too, but because a string can never change, a method cannot change the caller's text.

Returning an array

A method can return any type, including an array, so it can hand back several values at once:

import java.util.Arrays;

public class Main {
    static int[] minAndMax(int[] values) {
        int min = values[0], max = values[0];
        for (int v : values) {
            min = Math.min(min, v);
            max = Math.max(max, v);
        }
        return new int[] {min, max};
    }

    public static void main(String[] args) {
        int[] result = minAndMax(new int[] {7, 2, 9, 4});
        System.out.println(Arrays.toString(result));
    }
}
Output
[2, 9]

Varargs: any number of arguments

Three dots after the type (int... numbers) let a method accept any number of arguments of that type. Inside, the parameter is an ordinary array:

public class Main {
    static int sum(int... numbers) {
        int total = 0;
        for (int n : numbers) {
            total += n;
        }
        return total;
    }

    public static void main(String[] args) {
        System.out.println(sum());
        System.out.println(sum(4));
        System.out.println(sum(1, 2, 3, 4, 5));
    }
}
Output
0
4
15

printf and String.format use varargs; that is why they accept any number of values. A varargs parameter must be the last one in the list.

Practice exercises

  1. Write double average(double a, double b, double c) and print the average of three marks.
  2. Write a method that reverses an array in place and test it with {1, 2, 3, 4, 5}.
  3. Write int maxOf(int... numbers) that returns the largest of any number of values.