Skip to main content
A parameter in Java is a distinct kind of variable declared within the signature of a method, constructor, lambda expression, or catch block that serves as a placeholder for the data passed into the execution context during invocation. According to the Java Language Specification, parameters are strictly categorized as their own kinds of variables (e.g., method parameters, constructor parameters, exception-handler parameters), distinct from standard local variables, even though they share local scope. A parameter establishes a strict contract defining the data types and their order that the block expects to receive. The parameter identifier (name) is strictly for internal use within the block and is not part of the method signature or external contract.

Formal vs. Actual Parameters

In Java terminology, a strict distinction is made between the declaration and the invocation:
  • Formal Parameters: The variables defined in the method or constructor signature. They allocate memory on the stack frame when the method is called.
  • Actual Parameters (Arguments): The specific values or object references passed to the method during invocation.

Parameter Passing Mechanism: Pass-by-Value

Java strictly utilizes pass-by-value for all parameters. It never uses pass-by-reference, though the behavior differs depending on the data type being passed: 1. Primitive Types When a primitive data type (e.g., int, double, boolean) is passed as a parameter, Java creates a literal copy of the value. Reassigning or modifying the formal parameter within the method body has no effect on the actual parameter in the calling scope.
2. Reference Types When an object is passed as a parameter, Java passes a copy of the reference (the memory address pointing to the object on the heap), not a copy of the object itself.
  • Mutating the object’s internal state via the parameter will affect the original object.
  • Reassigning the parameter to a completely new object will not affect the original reference in the calling scope.

Variable-Length Arguments (Varargs)

Java supports methods that accept an arbitrary number of parameters of the same type using varargs syntax (...). Internally, the Java compiler translates a vararg parameter into an array of the specified type. A method can only have one vararg parameter, and it must be the final parameter in the method signature.

Final Parameters

Parameters can be declared with the final modifier. This prevents the reassignment of the formal parameter variable within the method body. For reference types, final does not make the referenced object immutable; it only prevents the parameter from pointing to a different memory address. The internal state of the object can still be mutated.

Lambda Expression Parameters

Lambda expressions introduce specialized parameter syntax. Lambda parameters can explicitly declare their types, infer their types from the functional interface context, omit parentheses when there is exactly one inferred-type parameter, or utilize the var keyword (introduced in Java 11) for implicitly typed parameters. The var keyword is particularly useful when applying annotations to inferred parameters.

Catch Block Parameters

Exception handling constructs utilize parameters to capture thrown exception instances. A catch block parameter acts as a variable initialized with the caught exception object. Since Java 7, a single catch block parameter can handle multiple exception types using the | symbol. In this context, | is not a bitwise operator; it is a syntactic separator used to denote a union of exception types (multi-catch). Using this union syntax implicitly makes the catch block parameter final.
Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More