HashTable Problem

I have an interesting question that implies the use of Hashtables ; I am developing for Nokia S40 (with conformance level 1.4)

As I expect the Hashtable will work:

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

However, I get the error message:

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

However, when I create a link to an object and pass the link, it works great! Why?

Working example:

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

Any explanations would be great!

+6
source share
6 answers

In my answer, I believe that your actual code was actually as follows:

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

This is the code that causes the error you described, i.e.

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

The reason is this:

  • Java 1.4 does not support generics , so Hashtable just works with Objects (both as keys and values)

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

Voila.

If you used Java 1.5+, the call will be table.put(Integer, String) on table.put(Integer, String)

BTW, do not use new Integer(1) , always prefers the static factory method Integer.valueOf(1) . You can avoid unnecessarily creating redundant classes. This is what autoboxing is compiled. See So: Static factory methods vs Instances (regular) constructors?

+3
source

From the error message you mentioned

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

It is clear that your compiler considers the Integer object as a primitive value immediately after its initialization. Namely, he immediately applies the unpacking . Perhaps this was done for optimization on mobile platforms, if I can find a link for it, I will update my answer.

+3
source

The problem with your code is that, as you mentioned, this is 1.4 conformance, which makes me think that you are compiling compatibility with 1.4 for it. 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 to do the same with javac --source 1.4 --target 1.4 then he will complain

+2
source

I ignore which JVM is used for Java, it develops in Nokia mobile phones (I would suggest Java ME), but in a typical Java SE environment your code should not give an error, but a warning: you did not use templates to build a HashTable, so the JVM ust assumes that your Integer and String are an Object class instead of real values.

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

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

As mentioned earlier, you probably don't have the auto-update feature introduced in java 1.5, since you are running at 1.4 compliance level. My suggestion sets your IDEA jdk to 1.4 instead of 1.7 which you are currently using.

0
source

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

-1
source

Source: https://habr.com/ru/post/972165/


All Articles