W3docs

Java Method Parameters

Pass values into Java methods through parameters, and understand the difference between parameters and arguments.

Parameters are the inputs a method declares; arguments are the values you supply when you call it. The compiler checks that the arguments line up with the parameters by type and position, then hands them to the method as local variables it can use in its body.

This chapter walks through that handoff in detail — the syntax, the types you can pass, and the rules Java applies to convert between them.

Declaring parameters

A method's parameter list is a comma-separated sequence of Type name pairs:

public static void greet(String name, int times) {
  for (int i = 0; i < times; i++) {
    System.out.println("Hello, " + name);
  }
}

A method can declare zero, one, or many parameters. Each one needs its own type — there's no shorthand like int a, b (that's int a followed by b, which won't compile).

Calling with arguments

At the call site, you pass values in the same order the parameters are declared:

greet("Ada", 3);    // name = "Ada", times = 3

The compiler checks two things:

  1. Arity — the number of arguments matches the number of parameters.
  2. Types — each argument's type is compatible with the matching parameter's type.

If either check fails, your program won't compile. Pass a String where an int is expected, and you'll get an error like incompatible types: String cannot be converted to int.

Parameters are local variables

Inside the method, each parameter behaves like a local variable that's been initialized to the argument value:

public static int doubleIt(int n) {
  n = n * 2;          // legal: n is just a local
  return n;
}

int x = 5;
int result = doubleIt(x);   // result is 10
System.out.println(x);       // still 5

Reassigning n inside doubleIt does not affect x at the call site. Each call gets its own fresh copy of the parameter. This is what people mean when they say Java is "pass-by-value" — every argument is copied into the parameter slot. The next chapters cover the implications, especially for object arguments.

Implicit widening

Java will automatically widen a smaller numeric type to a larger one to match the parameter:

public static double half(double x) {
  return x / 2;
}

int n = 7;
double result = half(n);   // n is widened from int to double

Widening is allowed because no information is lost. Narrowing is not implicit — passing a double to an int parameter requires a cast:

half(7.5);            // fine: double argument, double parameter
double d = 3.7;
int i = (int) d;      // explicit cast
half(i);              // also fine

The same widening rules apply as with assignment: byte → short → int → long → float → double, and char → int.

Object parameters

You can pass any object — String, an array, a custom class — by declaring a parameter of the matching reference type:

public static int firstLength(String[] words) {
  if (words.length == 0) return 0;
  return words[0].length();
}

String[] colors = {"red", "green", "blue"};
System.out.println(firstLength(colors));   // 3

The argument can also be null, in which case the parameter is null and any field access or method call on it throws NullPointerException. Methods that accept references usually check first or document that null is not allowed.

Passing null

null is assignment-compatible with every reference type, so you can pass it for any object parameter:

greet(null, 1);

If the method dereferences the parameter (name.length(), name.charAt(0), …), it throws. A defensive method handles null explicitly, often with a guard at the top:

public static void greet(String name, int times) {
  if (name == null) name = "stranger";
  for (int i = 0; i < times; i++) {
    System.out.println("Hello, " + name);
  }
}

Final parameters

You can mark a parameter final to forbid reassignment inside the method:

public static int doubleIt(final int n) {
  // n = n * 2;     // ERROR: cannot assign a value to final variable n
  return n * 2;
}

This doesn't change what the caller sees — only whether the body is allowed to reuse the parameter slot. Many style guides treat all parameters as if they were final even without the keyword; just compute new values rather than mutating the parameter.

Argument evaluation order

When you write f(a(), b(), c()), the arguments are evaluated left to right, all before f is called. If a() throws, b() and c() never run.

public static int log(String label, int value) {
  System.out.println(label + " = " + value);
  return value;
}

public static int sum(int a, int b, int c) {
  return a + b + c;
}

sum(log("a", 1), log("b", 2), log("c", 3));
// prints:
//   a = 1
//   b = 2
//   c = 3

This determinism is unlike C and C++; in Java the order is fixed and predictable.

A worked example

java— editable, runs on the server

What's next

You know how data gets into a method. Sometimes you want two methods with the same name but different inputs — say, one that accepts an int and another that accepts a String. Java lets you write both at once with method overloading.

Practice

Practice

What happens to a primitive variable when it's passed as an argument to a method?