java · level 1 · unit 7 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a slot where your method catches information.
Picture your method as a machine. Right now it does the same thing every time. Cut a slot in the front, and you can feed in something new on each go.
That slot is a parameter: a box in the method's brackets, with a type and a name, like String name in static void greet(String name). Inside the method, use it like any other box.
The value you drop in when you call is the . Call greet("Mia") and name holds "Mia". Call greet("Leo") and the same machine greets Leo instead.
public class Main {
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Mia");
greet("Leo");
}
} Need two slots? Split the parameters with commas, and the arguments too.
guess first
One machine, two arguments. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
public class Main {
static void stars(int count) {
for (int i = 1; i <= count; i++) {
System.out.print("*");
}
System.out.println();
}
public static void main(String[] args) {
stars(3);
stars(5);
}
}
your turn
Your turn. Run the birthday card machine.
Type a name between each pair of quotes and a whole number age after each comma. Then run it and watch one machine make two cards.
output
press run to see what happens
fix it
Rex is hungry, but this will not run. Can you fix it?
output
press run to see what happens
- Java cannot find a symbol on line 3. Which name has it never heard of?
- The parameter on line 2 is called pet, but line 3 uses a different name.
- Change animal to pet on line 3, then press run.
nailed it
your methods take orders now. :D
- A parameter is a slot with a type and a name.
- An argument is the value you drop in when you call.
- Arguments fill parameters in order: first into first.
new words in your collection
- parameter
- argument
This lesson isn’t saved. A free account remembers every lesson you finish.