I wonder why java.util.Hashtable
methods perform operations on elements array through locally assigned variable, instead of accessing class member directly. Does it has something to do with synchronization (keeping elements array in consistent state between method calls?)
For example : Entry<?,?> tab[] = table;
in
private void addEntry(int hash, K key, V value, int index) {
420 modCount++;
421
422 Entry<?,?> tab[] = table;
423 if (count >= threshold) {
424 // Rehash the table if the threshold is exceeded
425 rehash();
426
427 tab = table;
428 hash = key.hashCode();
429 index = (hash & 0x7FFFFFFF) % tab.length;
430 }
or in
456 public synchronized V put(K key, V value) {
457 // Make sure the value is not null
458 if (value == null) {
459 throw new NullPointerException();
460 }
461
462 // Makes sure the key is not already in the hashtable.
463 Entry<?,?> tab[] = table;
464 int hash = key.hashCode();
465 int index = (hash & 0x7FFFFFFF) % tab.length;
466 @SuppressWarnings("unchecked")
467 Entry<K,V> entry = (Entry<K,V>)tab[index];
468 for(; entry != null ; entry = entry.next) {
469 if ((entry.hash == hash) && entry.key.equals(key)) {
470 V old = entry.value;
471 entry.value = value;
472 return old;
473 }
474 }
475
476 addEntry(hash, key, value, index);
477 return null;
478 }