Unnecessary jbox unboxing - how to refactor code?

I have the following Java 8 code (simplified for demonstration):

public Double fooBar(int a) { return Double.valueOf(a); } 

Now IntelliJ IDEA tells me that I have an extra box in the return statement. I would understand this problem if a was double , but for int I feel like I need boxing to convert to double .

Is there a good way to reorganize this code that I don’t see right now, or is the error message from IntelliJ IDEA currently just not optimal?

thanks for the help

+5
source share
2 answers

If you use alt-enter to use the proposed "Delete Boxing" resolution, the result will be the following:

 public Double fooBar(int a) { return (double) a; } 

In this case, the conversion from int to double very explicit, while avoiding manual boxing.

+7
source

How about staying in a primitive area all the way?

 public double fooBar(int a) { return a; } 
+6
source

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


All Articles