Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I new a Hashtable, why the put method is invocated?

Tags:

java

hashtable

I am reading the code of Hashtable and am confused and have some questions. I coded like this:

Hashtable table = new Hashtable();
table.put(table, 1);
int code = table.hashCode();

I have two questions:

  1. When I invoke the hashCode method just like the third line code, why isn't it an endless loop? I think it is an endless loop.

  2. When I debug this code, I found that code new Hashtable() will cause the invocation of put method, why?

like image 588
huashui Avatar asked Mar 24 '23 01:03

huashui


1 Answers

  1. According to the OpenJDK source I'm reading, there's a guard written specifically to guard against the case where a Hashtable contains itself.

  2. I don't see any reference to put within the constructor. Do you have a trace you could post in your answer?

like image 141
Jeff Bowman Avatar answered Apr 01 '23 13:04

Jeff Bowman