No, because a
, b
and c
are of the type int
, which is a primitive type. In Java, everything that is passed as an argument to a function (including constructors) is passed by value (i.e. copied). Even a reference is passed by value (i.e. the object may not be copied, but the "pointer" to it is).
int a = 10;
foo(a); // "foo" recebe uma cópia de a
In the case of a ArrayList
We have an additional complication: he only accepts objects as list elements, not primitive types. In older versions of Java, it was even mandatory that you explicitly create a Integer
from their int
s before passing to ArrayList
:
int a = 10;
meuArrayList.add(new Integer(a));
From a certain version (I don’t quite remember which) Java implemented the functionality autoboxing - that allows you to use int
and Integer
as if it were only one thing (although "underneath the scenes" it keeps creating the object for you, so that it affects performance and memory usage):
int a = 10;
meuArrayList.add(a); // Na verdade, está sendo adicionado: new Integer(a)
int b = meuArrayList.get(0); // Na verdade, está sendo retornado:
// meuArrayList.get(0).intValue()
The only situation where a modification made outside the list affects the objects within the list is if the added objects are mutable, and you’re messing with the contents of them - not in their reference:
int[] a = new int[]{ 10, 20, 30 };
meuArrayList.add(a);
// Mexeu no conteúdo de a
a[1] = 40;
meuArrayList.get(0)[1]; // 40
// Mexeu na referência a
a = new int[]{ 50, 60, 70 };
meuArrayList.get(0)[1]; // 40 (continua sendo o array antigo)
In the case of autoboxing, however, there would be no problem even if a
, b
and c
were the type Integer
: since he is immutable. As we can not modify it once created, nothing you do outside the ArrayList
will affect the values within it.