No, because a
, b
and c
are of type int
, which is a primitive type . In Java, anything 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 ArrayList
we have an additional complication: it only accepts objects as list elements, not primitive types. In older Java versions, it was even mandatory to explicitly create a Integer
from your int
s before moving to ArrayList
:
int a = 10;
meuArrayList.add(new Integer(a));
From a certain version (I can not quite remember) Java has implemented the autoboxing feature - which allows you to use int
and Integer
as if (although "underneath the cloths" it continues to create the object for you, so this 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 out of the list affects the objects within the list is if the added objects are changeables , and you are tinkering with the content of them - not in your 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)
For autoboxing , however, there would be no problem even if a
, b
and c
were Integer
: since it is immutable . Since you can not modify it once it is created, nothing you do outside of ArrayList
will affect the values inside it.