Codementor Events

collection references in java

Published Oct 28, 2019
collection references in java

In java value are store in collection are by refernece . If two map are refer to same object . and if one object is become null it does not effect value of other one where updation in any value effect each other.

for Example

Hashmap<String,String> map = new Hashmap<String,String>();
Hashmap<String,String> map1= map;

if we add a value in map1 i.e

map1.add("name","amitrai");

Then value of map variable is aslo change but

if we make map1 null i.e

 map1 = null; 

it does not affect value of map because it store value by refrence

so if we print```
map.get("name");


 we do not get any exeception , it will simply print amit ........## 
Discover and read more posts from Amit Rai
get started
post commentsBe the first to share your opinion
Show more replies