The method calls the inefficient new constructor Integer (int); use Integer.valueOf (int) instead

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)); //[FindBugs] Method com.abc.xyz.Test.main(String[]) invokes inefficient new Integer(int) constructor; use Integer.valueOf(int) instead 

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)); // Unnecessary boxing to Integer 

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 ?

+5
source share
2 answers

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?

Yes, the compiler knows that m.put("first", a) only accepts objects and thus uses autoboxing. In terms of performance, using autoboxing or writing Integer.valueOf(a) does not make any difference.

On the other hand, new Integer(a) not much slower than Integer.valueOf(a) . The difference is that for small absolute values ​​(by default it will be from -128 to 127) Integer.valueOf(a) will use the cache, i.e. Will not create new objects all the time. For all other values, it will call new Integer(a) anyway.

Example:

Integer.valueOf(1) == Integer.valueOf(1) will give the true value Integer.valueOf(1000) == Integer.valueOf(1000) will give false values new Integer(1) == new Integer(1) will give false, since cache is not used here

2.) If instead of Map, if there is any data structure such as HashTable, then ???

Why are you asking about this? There is a HashTable , but since it is synchronized, it means more overhead than a HashMap , so if you don’t need a synchronization binding to a HashMap or TreeMap , if you need sorting.

3.) Why does the editor give an unnecessary box for Integer.

Probably only due to readability ( a shorter than Integer.valueOf(a) ).

4.) Why does m.put ("first", a) work? Since I am passing a primitive variable, and map put () takes an Object. So is it because of automatic boxing?

See 1

+4
source

4.) Why does m.put ("first", a) work? Since I am passing a primitive variable, and map put () takes an Object.

Autoboxing.

ints are automatically converted to Integer and vice versa (with a possible exception NullPointerException in this case)

3.) Why does the editor give an unnecessary box for Integer.

Because you do not need to write this piece of code. The compiler will do this for you.

This is usually more readable.

2.) If instead of Map, if there is any data structure such as HashTable, then the case is the same.

Yes, in JDK collections it only works with objects. This means that primitive types must be in the box. It has a low execution cost and a huge memory. An integer takes 300% more memory than an int.

You cannot escape this. The only way to avoid boxing overhead is to use specialized collections such as the GNU trove, which provide one class for the primitive type. Only useful if you plan on storing millions of primitive elements in a collection.

And finally, never write a new Integer (x). Integer.valueOf (x) does the same, but it maintains an internal cache to avoid creating new instances for some commonly used values.

+1
source

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


All Articles