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");
}
}Labas, Ona!
Labas, Jonas!
Labas, Rūta!
- In the method declaration,
String vardasis a parameter: a temporary place for one string. - The first call passes the argument Ona. Inside the method, the parameter
vardasthen stores Ona's name. - The method combines the text Labas, , the received name and an exclamation mark.
- The other two calls pass Jonas and Rūta to the same parameter.
- 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");
}
}Miestas: Kaunas
String miestasis a parameter because it appears in the method'srodytiMiesta()declaration.- Think of a parameter as an empty box waiting for text.
- Kaunas is an argument because this specific value is passed when the method is called.
- When the method is called, the argument Kaunas goes into the parameter
miestasbox. println()uses this value and displays Miestas: Kaunas.
| Concept | Where is it used? | Example |
|---|---|---|
| Parameter | In the method declaration, | String miestas |
| Argument | In 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);
}
}Asta yra 12 metų
Mantas yra 25 metų
Ona yra 34 metų
- The method has two parameters: the text parameter
vardasand the numeric parameteramzius. - The first call's arguments are Asta and
12. - Java matches arguments by position: the first to
vardasand the second toamzius. - The method combines both received values into one clear sentence.
- Each subsequent call passes a new name and age, producing three different lines.
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);
}
}Rūta yra 26 metų
- The variable
zmogausVardasstores the text Rūta. - The variables
gimimoMetaianddabartiniaiMetaistore the numbers2000and2026. - The first parameter receives the name value stored in the variable.
- The second parameter receives the result of
2026 - 2000, which is26. - 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);
}
}Jonas yra pilnametis žmogus
- The call passes two arguments: the name Jonas and the age
20. - Inside the method, the condition checks whether
amzius >= 18. - The comparison
20 >= 18istrue. - The first
ifblock therefore runs, displaying that Jonas is an adult. - If we passed the age
15, the condition would be false and the program would run theelseblock.
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.