Summary: in this tutorial, you will learn about Java passing parameters and how it works.
In Java, a method can have zero or more parameters. Sometimes, a method can accept a variable number of parameters via an varargs parameter.
When you pass arguments to the method, Java always uses something called “pass-by-value”. Let’s understand how it works.
Passing primitive values to methods
When you pass a value of primitive data type to a method, you pass it by value. It means that you pass a copy of the actual value to the method. Any changes made to the parameter within the method do not affect the original value outside the method.
For example:
public class App {
public static void increment(int n){
n = n + 1;
System.out.println("n = " + n);
}
public static void main(String[] args){
int a = 10;
System.out.println("Before method call: a = " +a);
increment(a);
System.out.println("After method call: a = " +a);
}
}
Code language: Java (java)
Output:
Before method call: a = 10
n = 11
After method call: a = 10
Code language: Java (java)
In this example, we define the increment()
method that increases the parameter n
by one and displays the new value on the screen.
In the main()
method:
First, declare the variable a and initialize its value to 10:
Second, display the value of the variable a before passing it to the increment()
method. We got 10 as expected.
Third, pass the variable a to the increment()
method.
Inside the increment()
method, Java makes a copy of the variable a
to the parameter n
and increases the value of the variable n
by one. It doesn’t change the value of the variable a
. In other words, it only works with the copy of the variable a
, which is n
:
When we display the value of n
inside the increment()
method, we get 11. However, the variable a
does not change. Its value remains the same.
Finally, display the value of the variable a after the method call. We also get 10.
This example illustrates that when you pass a value of a primitive type, Java uses the pass-by-value mechanism.
Passing a reference type to a method
When you pass an object to a method, you also pass it by value. Java still makes a copy of the reference, not the actual object. At this time, you have two references pointing to the same object.
If you change any members of the object via the copy of the reference inside the method, the changes are reflected in the original object outside the method.
The following program illustrates how to pass a reference to a method:
// Counter.java
public class Counter {
public int count = 0;
}
// App.java
public class App {
public static void increment(Counter counter){
counter.count++;
}
public static void main(String[] args){
var myCounter = new Counter();
System.out.println("Before method call: count=" + myCounter.count);
increment(myCounter);
System.out.println("After method call: count=" + myCounter.count);
}
}
Code language: Java (java)
Output:
Before method call: count=0
After method call: count=1
Code language: Java (java)
First, define a Counter
class that has a counter
field with the initial value of zero:
public class Counter {
public int count = 0;
}
Code language: Java (java)
Second, define a method that accepts an instance of the Counter
class. The method increases the count
field of the Counter
object by one:
public static void increment(Counter counter) {
counter.count++;
}
Code language: Java (java)
Third, inside the main()
method, we create a new Counter
object called myCounter
:
When we display the value of the count
field, it shows 0 as expected.
When we pass the myCounter
to the increment()
method, Java makes a copy of the myCounter
reference, not the actual counter
object.
As a result, we have two references myCounter
and counter
that refers to the same counter
object:
The increment()
method increases the value of the count
variable by one via the counter
reference:
After the increment()
method exits, the value of the count
member of the counter
object is reflected in the main()
method.
So when you pass a reference to a method, you can change the object referenced by the reference, and the change is reflected outside the method.
Summary
- Java always passes arguments to methods by values, not references.