简体   繁体   中英

HashTable Issue

I have an interesting question which entails the use of Hashtables ; I'm developing for S40 Nokia's (with compliance level 1.4)

How I expect the Hashtable to work:

Hashtable table = new Hashtable();
table.put(1, "Hello World");

However I get the error:

The method put(Object, Object) in the type Hashtable is not applicable for the arguments (int, String)

However when I create an object reference and pass the reference, it works fine! Why?!

Working example:

Hashtable table = new Hashtable();
Integer test = new Integer(1);
table.put(test, "Hello World");

Any explanations would be great!

In my answer I suppose that your actual code was in fact the following:

Hashtable table = new Hashtable();
table.put(1, "Hello World");

That's the code which causes the error you have described, ie

The method put(Object, Object) in the type Hashtable is not applicable for the arguments (int, String)

The reason is this:

  1. Java 1.4 does not support generics , so the Hashtable simply works with Objects (both as keys as well as values)

  2. Java 1.4 does not support autoboxing , so the code table.put(1, "Hello World") is not automatically autoboxed to table.put(Integer.valueOf(1), "Hello World") . Hence you are trying to call table.put(int, String) which is not compatible with Hashtable.put(Object, Object) .

Voila.

If you used Java 1.5+, the call would be autoboxed to table.put(Integer, String)

BTW, do not use new Integer(1) , always prefer the static factory method Integer.valueOf(1) . You may avoid unnecessary creation of redundant classes. This is what the autoboxing is compiled into. See this: Static factory methods vs Instance (normal) constructors?

From the error message you mentioned,

The method put(Object, Object) in the type Hashtable is not applicable for the arguments (int, String)

It is clear that your compiler treats the Integer object as a primitive value just after it is initialized. Namely it applies unboxing immediately. This might have been done for optimiziation in mobile platforms, if I can find a reference for it, I'll update my answer.

Problem with your code is that, as you mentioned, is 1.4 compliance, which makes me think you're compiling for it to be 1.4 compatible. Boxing / unboxing is a feature added in 1.5.

Just for you to confirm what I mean: try compiling your code with javac --source 1.5 --target 1.5 , it will compile fine, but try the same with javac --source 1.4 --target 1.4 then it will complain

I ignore which JVM is being used for Java develop in Nokia mobiles (I would assume a Java ME), but indeed in a typical Java SE environment your code should not give an error, but a warning: You have not used the templates to construct the HashTable, so the JVM ust assume your Integer and String are Object class, instead of the real values.

To avoid this warning, that for some reason your IDE reports as an error, use:

Hashtable<Integer, String> table = new Hashtable<Integer, String>();
table.put(new Integer(1),"Hello World");

As stated previously you probably don't have the autoboxing feature introduced in java 1.5 as you are running on compliance level 1.4. My suggestion is setting your IDEA jdk to 1.4 instead of 1.7 that you are currently using.

Integer is an object. int is not an object, it is a primitive. The object Integer wraps the primitive int . The put(Object, Object) method requires two objects, not a primitive and an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM