Skip to lesson

Parameters and arguments

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

GreetStudents.javaJava
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.");    }}
Output
Hello, Ravi! Welcome to class.
Hello, Anjali! Welcome to class.

What this code does

  • The first call stores "Ravi" in name and then runs the body.
  • The second call stores "Anjali", so the same method prints a different greeting.
  • Inside the body, name is used like any other String variable.

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.

The call printArea(12, 10) above the header printArea(int length, int width). The first argument 12 goes to the first parameter length, the second argument 10 to the second parameter width: matched by position.

Example 2 · Two parameters: area of a room

RoomArea.javaJava
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");    }}
Output
12 x 10 room: 120 sq ft
15 x 12 room: 180 sq ft

What this code does

  • In printArea(12, 10), 12 goes to length and 10 to width, because of their places.
  • int length, int width: each parameter has its own type, even when both are int.

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

GstCalc.javaJava
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);    }}
Output
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 / 2 is worked out first, and its answer, 7500.0, is what the method gets.
  • 500 is an int; Java widens it to the double 500.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.

Java
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 String

Find the bug

Spot the parameter bug

Select the line that is wrong.

This should print the area 120.

Round 1 of 3

Next step

Check your understanding

8 quick questions