Java Parameters
Write a method named greet that takes one word in. Inside the parentheses write String name. In the body print hi, a space, and then name: System.out.println("hi " + name);. From main call greet("lena");. The screen should show one line: hi lena. The letters lena are not hard-coded inside the method. They arrive through the call and sit in the name name for that pass. That hand-off is what this page teaches.
A name inside the parentheses
On the methods page the parentheses after ring stayed empty. The print was always ding. Here the parentheses hold a typed name so each call can send a different value. That typed name is a parameter. The value you write in the call is the argument for that pass.
Create a small program file named Tip.java. The public class on the first line must match the file name: public class Tip. Do not leave an older Chime or Pack class name in this file; the compiler expects the class name and the file name to agree.
Outside main, still inside the class braces, write:
static void greet(String name) {
System.out.println("hi " + name);
}
The header still starts with static void on this beginner shape. Between the parentheses sit String and then name — the type first, then the parameter name. The body uses name without quotes around it, joined to the fixed text "hi " with a plus. Quotes around "hi " mark the fixed letters. The bare name marks the value that arrived from the call. Save the file before you compile.
Tip.java with greet
This is the version you compile first. main holds the call with a value inside the parentheses. greet holds the print that uses that value.
public class Tip {
public static void main(String[] args) {
greet("lena");
}
static void greet(String name) {
System.out.println("hi " + name);
}
}
From the folder that holds Tip.java, run javac Tip.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Tip — not java Tip.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS; this page does not need a different shell spelling for either system. The program should print one line and stop: hi lena. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in the file. Delete that call and keep greet("lena"); plus the greet method above. A wrong screen line does not mean you need a new JDK install.
Walk one full run in order. Java starts at main. The call greet("lena"); sends the text lena into the parameter name. Inside greet, println builds hi, a space, and then that value, so the screen shows hi lena. Then greet finishes and main ends. You did not write System.out.println("hi lena"); inside main. You wrote the fixed part once under greet and passed lena from the call.
Same method, different words
Change only main so it calls greet twice with two different arguments:
public static void main(String[] args) {
greet("lena");
greet("sam");
}
Leave the greet method itself alone. Compile and run again: javac Tip.java, then java Tip. The screen should show two lines: hi lena, then hi sam. The first call fills name with lena. The second call fills name with sam. You did not copy the println under greet. You changed only the values inside the calls.
If you call greet() with empty parentheses while the header still says String name, javac writes an error — the call must supply a String. If you leave an old ring(); with empty parentheses from the Chime page in the same main, that call does not match greet, and a leftover ring method may still print ding beside your hi lines. Clear main to the greet calls this lesson needs.
A whole number as the value
Parameters are not only for text. Write a second method that takes an int:
static void show(int n) {
System.out.println(n);
}
Call it from main with whole numbers:
show(9);
show(4);
public class Tip {
public static void main(String[] args) {
show(9);
show(4);
}
static void show(int n) {
System.out.println(n);
}
}
Compile and run: javac Tip.java, then java Tip. The screen should show two lines: 9, then 4. No quotes wrap 9 or 4 in the call — they are int values, not text. Inside show, println(n) writes the number that arrived. After you have seen those two lines, you can put the greet demo back. Both versions are valid Java. They teach the same idea: the call sends a value, the parameter holds it for the body.
Read the int run once more. The first call sets n to 9 and prints 9. The second call sets n to 4 and prints 4. The name n is declared once in the header as int n. Each call gives it a new value for that pass. Passing a quoted word into show, such as show("nine");, does not match int on this shape — javac writes an error. Passing a bare number into greet, such as greet(3);, does not match String — javac writes an error again. Match the argument type to the parameter type.
A common mix-up is printing the parameter’s name as text: System.out.println("name");. That writes the letters n-a-m-e. It does not write lena. Keep println("hi " + name) without quotes around name when you want the argument’s letters on the screen. Another mix-up is declaring the parameter in main instead of in the method header. The typed name belongs inside greet(...) or show(...), not inside main’s parentheses on this page. main’s String[] args is a different topic for a later session; do not rename args to name and expect greet to see it.
Where this leaves you
You can put String name or int n inside a method’s parentheses, pass a matching value in the call, and print that value from the body. Two calls with two arguments print two different lines without copying the method. void still means nothing comes back through the call. The next page is return, where a method hands a value back to main instead of only printing inside the body.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top