Core Java Fundamentals ยท Methods
Java always copies the argument. For an int that is the number; for an array it is the reference, so the method can change the array itself.
public class PassByValueDemo {
static void addTen(int n) {
n = n + 10;
}
static void addItem(int[] items) {
items[0] = items[0] + 10;
}
public static void main(String[] args) {
int score = 5;
addTen(score);
int[] scores = {5, 6};
addItem(scores);
System.out.println(score + " " + scores[0]);
}
}main. args holds its command-line arguments (none here). score is created on the stack with the value 5.addTen ends; its copy is gone and score is still 5.scores.items points to the same array as scores.addItem() has finished, so its variables are gone.5 15.