Learn Java
import com.learnjava.methods.MethodParametersReturn;

Method Parameters & Return Types in Java

@Concept

Parameters are the named inputs a method declares in its signature — each has its own type, and calling the method must supply an argument matching that type for every parameter, in order. A method's return type declares what kind of value it hands back to its caller; void means it hands back nothing at all, and any other type requires every code path through the method to end in a return statement carrying a value of that type.

Java passes arguments by value. For a primitive, that means the method receives a copy of the number itself, so reassigning the parameter inside the method never affects the caller's original variable. For an object reference, the method receives a copy of the reference, still pointing to the same object — so mutating that object's fields is visible to the caller, but reassigning the parameter to point at a different object is not.

@HowItWorks
1

Declaring parameters

Each parameter in a method's signature has its own type and name, listed in the order arguments must be supplied.

2

Declaring a return type

void means the method returns nothing; any other type requires every path through the method to return a matching value.

3

Passing primitives by value

The method receives a copy of a primitive argument, so changes to the parameter inside the method never affect the caller's original variable.

4

Passing objects by value, too

The reference itself is copied, not the object, so mutating the object's fields is visible outside, but reassigning the parameter to a new object is not.

5

Multiple parameters, one call

Arguments are matched to parameters by position, not by name, so order matters.

@Example
public class ParamsDemo {
static int square(int n) {
n = n * n;
return n;
}
 
public static void main(String[] args) {
int original = 5;
int result = square(original);
System.out.println("Original: " + original);
System.out.println("Result: " + result);
}
}
Output
Original: 5
Result: 25
1square receives a copy of original's value, 5, into its own parameter n.
2Reassigning n inside square only changes that local copy — it has no effect on original back in main.
3return n; hands the new value, 25, back to the caller as the method's result.
4Both println calls confirm original is untouched while result correctly holds the squared value.
@UseCases
Writing a method that computes and returns a value without modifying its caller's variables
Understanding why passing an int to a method never lets that method change the caller's original variable
Recognizing that mutating an object passed as an argument does affect the caller, unlike a primitive
Designing a method's return type to match exactly what its caller actually needs back

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.