Codementor Events

Store Value By reference in collection (Java)

Published May 04, 2020Last updated May 06, 2020
Store Value By reference in collection (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 anay 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