syker
syker

Reputation: 11282

Return Value from Hashtable get function is a Reference?

Hashtable<String, Hashtable<String, HashSet<String>>> test = 
        new Hashtable<String, Hashtable<String, HashSet<String>>>();
test.put("1", new Hashtable<String, HashSet<String>>());
Hashtable<String, HashSet<String>> actual = test.get("1");
actual.put("3", new HashSet<String>());
//test.put("1", actual);
HashSet<String> expected = test.get("1").get("3");
if ( expected == null ) {
 System.out.println("DIE");      
} 

Based on the code above, I thought DIE would be printed out. But apparently, actual is a reference to the object inside test still. I was under the impression that I had to "put" back actual into test (shown by the line that had been commented out). Is there authoritative documentation on whether actual is a reference or not?

Upvotes: 0

Views: 2261

Answers (1)

casablanca
casablanca

Reputation: 70721

Objects are always passed as a reference in Java. From chapter 4 of JLS:

The reference types are class types, interface types, and array types. ... The reference values (often just references) are pointers to these objects.

Upvotes: 2

Related Questions