Java – will the result of modifying getter affect the object itself?

I have a question about using getter methods in Java

My question is:

Will the actual array list object be modified ("test 1" removed from it)? I think I've seen it in this place, but I think getter only provides a copy of the object Not reference it If it works in this way (as a reference), it will also work (the ArrayList object of the test class is also changed)?:

Solution

Java returns a reference to array, so it will not be a copy, it will modify list In general, unless it is a primitive type (int, float, etc.), you will get a reference to the object

If you want to return a copy, you must explicitly copy the array yourself

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>