JAVA METHODS

Java method parameters and arguments

Parameters pass data to a method. They let the same method perform an action with different values, avoiding a separate implementation for every case.

What is a parameter?

Parameters appear in parentheses in a method declaration. Each has a type and name. Inside the method, a parameter acts as a local variable.

public class Main {
    static void pasisveikinti(String vardas) {
        System.out.println("Labas, " + vardas + "!");
    }

    public static void main(String[] args) {
        pasisveikinti("Ona");
        pasisveikinti("Jonas");
        pasisveikinti("Rūta");
    }
}
Console output

Labas, Ona!
Labas, Jonas!
Labas, Rūta!

  1. In the method declaration, String vardas is a parameter: a temporary place for one string.
  2. The first call passes the argument Ona. Inside the method, the parameter vardas then stores Ona's name.
  3. The method combines the text Labas, , the received name and an exclamation mark.
  4. The other two calls pass Jonas and Rūta to the same parameter.
  5. The same method code runs three times, each time with a different name.

A parameter and an argument

public class Main {
    static void rodytiMiesta(String miestas) {
        System.out.println("Miestas: " + miestas);
    }

    public static void main(String[] args) {
        rodytiMiesta("Kaunas");
    }
}
Console output

Miestas: Kaunas

  1. String miestas is a parameter because it appears in the method's rodytiMiesta() declaration.
  2. Think of a parameter as an empty box waiting for text.
  3. Kaunas is an argument because this specific value is passed when the method is called.
  4. When the method is called, the argument Kaunas goes into the parameter miestas box.
  5. println() uses this value and displays Miestas: Kaunas.
The difference between a parameter and an argument
ConceptWhere is it used?Example
ParameterIn the method declaration,String miestas
ArgumentIn a method call"Kaunas"

When a method is called, each argument value is assigned to the corresponding parameter.

Multiple parameters

A method can have multiple parameters. Separate them with commas in both the declaration and the call:

public class Main {
    static void rodytiZmogu(String vardas, int amzius) {
        System.out.println(vardas + " yra " + amzius + " metų");
    }

    public static void main(String[] args) {
        rodytiZmogu("Asta", 12);
        rodytiZmogu("Mantas", 25);
        rodytiZmogu("Ona", 34);
    }
}
Console output

Asta yra 12 metų
Mantas yra 25 metų
Ona yra 34 metų

  1. The method has two parameters: the text parameter vardas and the numeric parameter amzius.
  2. The first call's arguments are Asta and 12.
  3. Java matches arguments by position: the first to vardasand the second to amzius.
  4. The method combines both received values into one clear sentence.
  5. Each subsequent call passes a new name and age, producing three different lines.
Argument count, order and types must match the parameters.

rodytiZmogu("Asta", 12) is correct, while rodytiZmogu(12, "Asta") does not match the declared order.

Arguments need not be literal values

A method call can pass variables or results of expressions with compatible types:

public class Main {
    static void rodytiZmogu(String vardas, int amzius) {
        System.out.println(vardas + " yra " + amzius + " metų");
    }

    public static void main(String[] args) {
        String zmogausVardas = "Rūta";
        int gimimoMetai = 2000;
        int dabartiniaiMetai = 2026;

        rodytiZmogu(zmogausVardas, dabartiniaiMetai - gimimoMetai);
    }
}
Console output

Rūta yra 26 metų

  1. The variable zmogausVardas stores the text Rūta.
  2. The variables gimimoMetai and dabartiniaiMetai store the numbers 2000 and 2026.
  3. The first parameter receives the name value stored in the variable.
  4. The second parameter receives the result of 2026 - 2000 , which is 26.
  5. The method receives the final values and displays Rūta yra 26 metų.

Conditions inside a method

A parameter can be used in calculations, loops and conditional statements:

public class Main {
    static void tikrintiPilnametyste(String vardas, int amzius) {
        if (amzius >= 18) {
            System.out.println(vardas + " yra pilnametis žmogus");
        } else {
            System.out.println(vardas + " dar nėra pilnametis žmogus");
        }
    }

    public static void main(String[] args) {
        tikrintiPilnametyste("Jonas", 20);
    }
}
Console output

Jonas yra pilnametis žmogus

  1. The call passes two arguments: the name Jonas and the age 20.
  2. Inside the method, the condition checks whether amzius >= 18.
  3. The comparison 20 >= 18 is true.
  4. The first if block therefore runs, displaying that Jonas is an adult.
  5. If we passed the age 15, the condition would be false and the program would run the else block.

Parameter scope

A parameter is accessible only inside the method where it is declared. Its local value is no longer retained after the method finishes. The next call receives a new argument and a new parameter value.