Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How objects are stored in arraylist

 
Roshnara Rose
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to know how added objects are stored in arraylist.
Java passes objects by reference. So i added an object to arraylist and then set it to null. The arraylist has it.
Is pass by value happening here? Is the pass by reference is only for user defined methods?

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything in Java is passed by value. The thing is, for objects, it is the reference to the object that is copied. So in your example, the ArrayList has a copy of the reference to the object, so after you set your own reference to null there is still the ArrayList reference to the object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic