Question on Map.Entry

A

ankur

I was reading the code for HashMap.java from the source and did not
understand what is the object type of variable table in the default
constructor of HashMap object.

So in the class HashMap.java the follwoing appears:

transient Entry[] table;

public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
threshold = (int)(DEFAULT_INITIAL_CAPACITY *
DEFAULT_LOAD_FACTOR);
table = new Entry[DEFAULT_INITIAL_CAPACITY]; //What is the
object type here ???
init();
}

Since Entry is an Interface it does not have a constructor ...then how
is the table variable initialized in the deafault HashMap constructor
above ?

Thanks,
Ankur
 
E

Eric Sosman

ankur said:
I was reading the code for HashMap.java from the source and did not
understand what is the object type of variable table in the default
constructor of HashMap object.

So in the class HashMap.java the follwoing appears:

transient Entry[] table;

public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
threshold = (int)(DEFAULT_INITIAL_CAPACITY *
DEFAULT_LOAD_FACTOR);
table = new Entry[DEFAULT_INITIAL_CAPACITY]; //What is the
object type here ???
init();
}

Since Entry is an Interface it does not have a constructor ...then how
is the table variable initialized in the deafault HashMap constructor
above ?

Map.Entry is, as you say, an interface. But look more
closely in the HashMap source and you'll find

static class Entry<K,V> implements Map.Entry<K,V> {
...

So: The answer to the question you asked ("What is the object
type here?") is "HashMap.Entry[]" and the answer to the question
you probably meant to ask ("What is the element type of this
array?") is "HashMap.Entry".
 
A

ankur

ankur said:
I was reading the code for HashMap.java from the source and did not
understand what is the object type of variable table in the default
constructor of HashMap object.
So in the class HashMap.java the follwoing appears:
transient Entry[] table;
 public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        threshold = (int)(DEFAULT_INITIAL_CAPACITY *
DEFAULT_LOAD_FACTOR);
        table = new Entry[DEFAULT_INITIAL_CAPACITY]; //What is the
object type here ???
        init();
    }
Since Entry is an Interface it does not have a constructor ...then how
is the table variable initialized in the deafault HashMap constructor
above ?

Creating the Entry[] array does not involve creation of any Entry
objects at all, just an array structure and DEFAULT_INITIAL_CAPACITY
null references. Anything assigned to one of those elements has to be
either null or a reference to an object whose class implements Entry.

Patricia- Hide quoted text -

- Show quoted text -

Yes, I got it. Thank you. I missed the nested static class Entry.

Thanks,
Ankur
 
M

Mark Space

ankur said:
Yes, I got it.



Are you sure? What's the type of the objects created below, then?


java.util.List<?>[] listArray = new java.util.List<?>[10];


Yes, that compiles.
 
A

Arne Vajhøj

Mark said:
ankur said:
Yes, I got it.

Are you sure? What's the type of the objects created below, then?

java.util.List<?>[] listArray = new java.util.List<?>[10];

Yes, that compiles.

Trick question.

There are not created object*s* only one object.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top