Why do arrays change in method calls?

The fyeah variable in your first example contains a reference to an array (not an array), while the fyeah integer in your second example contains an integer.

Since Java passes everything by value the following will happen:

In the array case: A copy of the array reference will be sent, and the original array will be changed.

In the int case: A copy of the integer will be changed, and the original integer will not be changed.


It's because your int is a primitive and the method smth creates a local copy which is why it doesn't print the way you want. Objects are passed by value as well, but a value to the pointer in memory. So when it is changed, the pointer stays throughout both methods and you see the change. Read More Here