My goal is to optimize the application code. My code is as follows:
int a = 10; Map<String , Integer> myMap = new TreeMap<>(); myMap.put("first" , new Integer(a));
When I did static analysis using Findbugs in Netbeans, it shows that there is a warning / error, for example, " The method calls the inefficient new Integer (int) constructor; instead use Integer.valueOf (int) instead of ").
I know the difference between the new Integer (int) vs Integer.valueOf (int).
One creates an additional object, and the other does not. Other caches are also not cached.
So, I changed my code like this ...
m.put("first" , Integer.valueOf(a));
but again, it also gives the warning β Optional box for Integer β (not editor using findbugs).
So, I changed it again like this ....
m.put("first" , a); //No warning at all...
which finally gives a warning altogether.
My question is:
1.) This link assumes that internally (by the compiler) m.put ("first", a); converted to m.put ("first", Integer.valueOf (a));
(In this example, there is a List-ArrayList, and here we have Map-TreeMap ... FYI). then why does the editor give a warning? What should I do? what is optimized?
2.) If instead of Map , if there is any data structure, for example HashTable , then <
3.) Why does the editor give an unnecessary box for Integer.
4.) Why does m.put ("first", a) work ? Since I am passing a primitive variable , and map put () accepts only Object . Does this happen due to automatic boxing ?