java - Is it inefficient to reference a hashmap in another class multiple times? -


class a

class {     public hashmap <integer,double> myhashmap;  public a(){     myhashmap = new hashmap() } } 

class b

class b {     private aninstanceofa;  public b(a a) {     this.aninstanceofa = a; }   amethod(){        aninstanceofa.myhashmap.get(1); <--getting hashmap value key = 1        //proceed use value, instead of storing variable       // use aninstanceofa.myhashmap.get(1) each time need value. } 

in amethod() use aninstanceofa.myhashmap.get(1) value key = 1. multiple times in amethod() , i'm wondering if there difference in efficiency between using aninstanceofa.myhashmap.get(1) multiple times or assigning variable , using assigned variable multiple times.

i.e

amethod(){      thevalue = aninstanceofa.myhashmap.get(1);       //proceed use thevalue in calculations. there difference in efficiency? } 

in theory jvm can optimise away difference small (compared rest of program doing). prefer make local variable believe makes code clearer (as can give meaningful name)

i suggest believe simpler , clearer, unless have measured performance difference.


Comments