Java - 列表中的对象是按值还是引用保存的? [英] Java - Are objects in a list held by value or reference?
问题描述
简单的问题,Java中的列表是否按值或引用保存对象?如果我将一个对象放在一个列表中,然后更改其中一个值,那么列表中的那个值是否具有更新的值?
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?
推荐答案
Java中的列表是按值还是引用保存对象?
does a list in Java hold objects by value or reference?
List 是一个包含对象引用的有序集合。
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?
您只能更改/更新可变对象。您无法通过其引用更新 immutable (例如 List< String>
)对象。
You can change/update only mutable objects. You cannot update immutable (e.g List<String>
) objects via their references.
这篇关于Java - 列表中的对象是按值还是引用保存的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!