Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Are objects in a list held by value or reference?

Simple question, does a list in Java hold objects by value or reference? If I place an object in a list and later on change one of its values, is the one in the list going to have the updated value?

like image 395
WildBamaBoy Avatar asked Dec 21 '22 03:12

WildBamaBoy


1 Answers

does a list in Java hold objects by value or reference?

The List is an ordered collection that contains object references.

If I place an object in a list and later on change one of its values, is the one in the list going to have the updated value?

You can change/update only mutable objects. You cannot update immutable (e.g List<String>) objects via their references.

like image 142
KV Prajapati Avatar answered Dec 24 '22 02:12

KV Prajapati