课程: Java Memory Management: Values and References

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Collections and escaping references

Collections and escaping references

- [Micah] Collections can hold multiple objects, and they do so by holding multiple references to all these objects. It is common that when you return the collection from a method that you make a clone of this collection because a collection is an object itself. And when you return a collection, sometimes you don't want to share to the reference. And if that's the case, you have to make a clone. 'Cause if you make a clone, you cannot modify the collection from another place using reference. So for example, something gets added or something gets removed, this won't happen to your original place because it's a new reference and a new object. So in these cases, you'll return a clone of your collection. Let's have a look at an example in IntelliJ to see this in action. All right, so I've made little demo here, and clearly, this is poorly structured code and this looks terrible, but it's just for the purpose of explaining what's going on with collections and references and more…

内容