Parameters and arguments
20 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A parameter is a variable in a method's brackets that receives a value. The value you send when you call the method is the argument.
Sending a value into a method
A greet() method can only print one fixed name, but every student has a different one. A parameter, like String name in the brackets, receives the value the call sends: the argument.
Example 1 · One parameter: greet any student
public class GreetStudents { public static void main(String[] args) { greet("Ravi"); greet("Anjali"); }
static void greet(String name) { System.out.println("Hello, " + name + "! Welcome to class."); }}Hello, Ravi! Welcome to class. Hello, Anjali! Welcome to class.
What this code does
- The first call stores
"Ravi"innameand then runs the body. - The second call stores
"Anjali", so the same method prints a different greeting. - Inside the body,
nameis used like any otherStringvariable.
More than one parameter
A room's area needs a length and a width. List several parameters with commas, each with its own type; the arguments fill them by position, left to right.
Example 2 · Two parameters: area of a room
public class RoomArea { public static void main(String[] args) { printArea(12, 10); printArea(15, 12); }
static void printArea(int length, int width) { int area = length * width; System.out.println(length + " x " + width + " room: " + area + " sq ft"); }}12 x 10 room: 120 sq ft 15 x 12 room: 180 sq ft
What this code does
- In
printArea(12, 10), 12 goes tolengthand 10 towidth, because of their places. int length, int width: each parameter has its own type, even when both areint.
Values, variables or expressions
The price of a phone is usually in a variable, not typed into the call. An argument can be any value of the right type: a number, a variable or an expression. Only its value is sent.
Example 3 · A variable or an expression as the argument
public class GstCalc { public static void main(String[] args) { double phonePrice = 15000; printGst(phonePrice); printGst(phonePrice / 2); printGst(500); }
static void printGst(double price) { double gst = price * 18 / 100; System.out.println("GST on Rs. " + price + " is Rs. " + gst); }}GST on Rs. 15000.0 is Rs. 2700.0 GST on Rs. 7500.0 is Rs. 1350.0 GST on Rs. 500.0 is Rs. 90.0
What this code does
printGst(phonePrice)sends the value 15000.0. The variable does not need the parameter's name,price.phonePrice / 2is worked out first, and its answer, 7500.0, is what the method gets.500is anint; Java widens it to thedouble500.0, as in Type conversion and casting (opens in a new tab).
The other way round does not compile: a double argument for an int parameter gives incompatible types: possible lossy conversion from double to int.
Arguments must match
Leave out an argument, or send the wrong kind, and the call no longer fits the method. A call must match the parameters in number, order and type.
static void printResult(String name, int marks) { ... }
printResult("Ravi", 72); // fineprintResult("Ravi"); // error: cannot be applied to given typesprintResult(72, "Ravi"); // error: int cannot be converted to StringFind the bug
Spot the parameter bug
Select the line that is wrong.
This should print the area 120.
Round 1 of 3Next step
Check your understanding
8 quick questions