Java Tutorial
Java Method Parameters
Parameters receive the values you pass at the call site. Primitives are copied; object references are shared — so changes to the object are visible to the caller.
Pass arguments into a method
The names in the method header are parameters. The values you write in the call are arguments. Inside the method, parameters behave like local variables that start with those argument values.
Example
public class Main {
static void greet(String name, int times) {
for (int i = 0; i < times; i++) {
System.out.println("Hi, " + name);
}
}
public static void main(String[] args) {
greet("Ada", 2);
}
}Primitives are copied
When you pass an int, the method gets its own copy. Reassigning the parameter does not change the caller's variable. Java always passes by value — for primitives, the value is the number itself.
Example
public class Main {
static void bump(int n) {
n = n + 1;
System.out.println("inside: " + n);
}
public static void main(String[] args) {
int x = 5;
bump(x);
System.out.println("outside: " + x); // still 5
}
}Object references are shared
For objects, the value that is copied is the reference. The method and the caller point at the same object, so mutating fields inside the method is visible outside. Reassigning the parameter to a new object does not rebind the caller's variable.
Example
class Counter {
int n;
}
public class Main {
static void bump(Counter c) {
c.n = c.n + 1;
}
public static void main(String[] args) {
Counter c = new Counter();
c.n = 5;
bump(c);
System.out.println(c.n); // 6 — same object
}
}Java has no default parameter values. If you want optional arguments, write overloads — separate methods with the same name and different parameter lists — covered in the next lesson.
Try It Yourself
Exercise: Write static int scale(int value, int factor) that returnsvalue * factor. Call it from main and print the result. Confirm the originalvalue variable is unchanged.
Show solution
public class Main {
static int scale(int value, int factor) {
return value * factor;
}
public static void main(String[] args) {
int value = 4;
int result = scale(value, 3);
System.out.println(result); // 12
System.out.println(value); // 4
}
}The method returns a new number; the caller's value stays 4 because primitives are copied.
Key Takeaways
- Parameters receive the arguments you pass at the call site.
- Primitives are copied; reassigning a parameter does not change the caller.
- Object references are copied, but they still point at the same object — field changes show through.
- Java has no default arguments; use overloading for alternate parameter lists.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Physics
Pass mass and height
Parameters carry the numbers into the method. mgh needs both m and h; the signature makes that contract visible.
E = mgh
Example
public class Main {
static double pe(double m, double h) {
return m * 9.81 * h;
}
public static void main(String[] args) {
System.out.println(pe(2.0, 1.5) + " J");
}
}