Java Method Arguments Explained: Passing Values, Types & Examples

0

A method declaration tells Java what kind of input a method expects, but the method becomes useful when actual values are supplied to it. These actual values are called arguments.

If parameters are the input slots defined by a method, arguments are the real values placed into those slots when the method is called. Understanding this distinction is essential for writing and reading Java programs correctly.

What Is an Argument?

An argument is the actual value, variable, expression, or object supplied to a method when the method is invoked.

static void greet(String name) {
    System.out.println("Hello, " + name);
}

greet("Rahul");

Here, name is the parameter, while "Rahul" is the argument.

Remember: A parameter is declared in the method definition. An argument is supplied when the method is called.

Parameter vs Argument

Feature Parameter Argument
Location Method declaration Method invocation
Purpose Defines expected input Provides actual input
Example String name "Rahul"
Role Receives the supplied value Supplies the value

Simple Argument Example

Consider a method that accepts an integer argument:

static void printNumber(int number) {
    System.out.println("Number: " + number);
}

printNumber(25);

The method declares number as its parameter. When printNumber(25) is executed, the value 25 is supplied as the argument.

Multiple Arguments

A method can accept multiple arguments. The arguments are matched with parameters according to their position.

static void displayStudent(String name, int age) {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
}

displayStudent("Anita", 22);

The first argument, "Anita", is passed to the name parameter. The second argument, 22, is passed to the age parameter.

Important: When a method has multiple parameters, the number, order, and compatible types of the arguments must match the method's parameter list.

Arguments Can Be Variables

An argument does not have to be a literal value. You can pass a variable as an argument.

static void showAge(int age) {
    System.out.println("Age: " + age);
}

int studentAge = 21;

showAge(studentAge);

Here, studentAge is the argument. Its current value is supplied to the age parameter.

Arguments Can Be Expressions

Java also allows expressions to be used as arguments. The expression is evaluated before its resulting value is passed to the method.

static void showResult(int value) {
    System.out.println("Result: " + value);
}

showResult(10 + 20);

Java evaluates 10 + 20 first and passes the resulting value, 30, to the method.

Arguments with Method Calls

A method call can also use another method's returned value as an argument. This allows methods to work together naturally.

static int calculateTotal(int price, int quantity) {
    return price * quantity;
}

static void displayTotal(int total) {
    System.out.println("Total: " + total);
}

displayTotal(calculateTotal(100, 3));

The inner method calculates the total first. Its returned value then becomes the argument for displayTotal().

Arguments Must Match Parameter Types

Java is strongly typed, so an argument must be compatible with the corresponding parameter type.

static void printAge(int age) {
    System.out.println(age);
}

printAge(25);

This works because 25 is an integer.

But supplying an incompatible value can cause a compilation error.

static void printAge(int age) {
    System.out.println(age);
}

// Compilation error
printAge("Twenty");

The method expects an int, but the argument is a String.

Compatible Type Conversion

Java can perform certain implicit conversions when the argument type can safely fit into the parameter type.

static void showNumber(double number) {
    System.out.println(number);
}

showNumber(25);

The integer value 25 can be widened to a double, so the call is valid.

showNumber(25.75);

This call is also valid because the supplied argument is already a double.

Number of Arguments Must Match

When a method expects a specific number of parameters, the method call normally needs the corresponding number of arguments.

static int add(int a, int b) {
    return a + b;
}

int result = add(10, 20);

The method expects two parameters, so two arguments are supplied.

// Compilation error
int result = add(10);

The call above provides only one argument for a method that expects two parameters.

Order of Arguments Matters

Arguments are assigned to parameters from left to right. Therefore, their order can affect the result.

static void showDetails(String name, int age) {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
}

showDetails("Rahul", 23);

The first argument goes to name and the second goes to age.

// Invalid because the argument order does not match
showDetails(23, "Rahul");

The types are reversed, so Java cannot match the arguments with the declared parameters.

Passing String Arguments

Strings are frequently passed to methods in Java applications.

static void welcome(String username) {
    System.out.println("Welcome, " + username);
}

welcome("Priya");
welcome("Amit");

The same method can process different string values without duplicating the method's logic.

Passing Boolean Arguments

Boolean arguments are useful when a method needs to make decisions based on a true or false condition.

static void checkAccess(boolean loggedIn) {
    if (loggedIn) {
        System.out.println("Access granted");
    } else {
        System.out.println("Please log in");
    }
}

checkAccess(true);
checkAccess(false);

The same method handles both states by receiving a different argument.

Passing Arrays as Arguments

An array can also be supplied as an argument to a method.

static void printMarks(int[] marks) {
    for (int mark : marks) {
        System.out.println(mark);
    }
}

int[] studentMarks = {85, 90, 78, 92};

printMarks(studentMarks);

The array variable is passed to the method, allowing the method to work with all of its elements.

Passing Objects as Arguments

Methods can also receive objects as arguments. This becomes especially important when building object-oriented applications.

class Student {
    String name;
}

static void displayStudent(Student student) {
    System.out.println(student.name);
}

Student s = new Student();
s.name = "Rahul";

displayStudent(s);

The object reference is supplied as the argument, allowing the method to access the object's available members.

Arguments and Expressions

You can pass arithmetic, comparison, or other valid expressions as arguments when the resulting value is compatible with the parameter.

static void display(int value) {
    System.out.println(value);
}

int a = 10;
int b = 5;

display(a + b);
display(a * b);
display(a - b);

Each expression is evaluated before its result is supplied to the method.

Arguments and Pass by Value

Java always uses pass by value when supplying arguments to methods. For primitive values, the value itself is copied. For objects, the value of the reference is copied.

This distinction becomes particularly important when a method modifies data associated with an object. The complete pass-by-value behavior will be explored separately because it is one of the most frequently misunderstood topics in Java.

Remember: Java does not use pass-by-reference for method arguments. It always passes a copy of the value.

Common Beginner Mistakes

  • Confusing an argument with a parameter.
  • Passing the wrong number of arguments.
  • Passing arguments in the wrong order.
  • Passing an incompatible data type.
  • Assuming Java passes primitive variables by reference.
  • Forgetting that expressions are evaluated before their resulting values are passed.

Best Practices

  • Pass only the data a method actually needs.
  • Keep argument order logical and consistent.
  • Use descriptive variables when complex expressions would make a method call difficult to read.
  • Prefer clear method signatures that make valid calls easy to understand.
  • Be especially careful when passing mutable objects because the method can work with the referenced object's state.

Interview Insight

A common interview question is: “What is an argument in Java?” A strong answer is: “An argument is the actual value or expression supplied to a method when it is invoked. It is matched with the corresponding parameter according to position and compatible type.”

Another common question is: “Does Java pass arguments by value or reference?” The correct answer is that Java is always pass-by-value. When an object is supplied, the copied value is the reference to that object, which is why object behavior can sometimes appear similar to pass-by-reference.

Quick Revision

Concept Key Point
Argument The actual value supplied during a method invocation.
Parameter The variable declared by the method to receive an argument.
Argument order Arguments are matched with parameters according to their position.
Type compatibility An argument must be compatible with the corresponding parameter type.
Expression argument A valid expression can be evaluated and its result passed to a method.
Object argument A method can receive an object reference as an argument.
Pass by value Java passes a copy of each argument's value to the method.

Arguments are the actual inputs that make reusable Java methods practical. Once you understand how arguments are matched with parameters, the next step is learning how methods can send information back to their callers through return values.

Post a Comment

0Comments
Post a Comment (0)