Why doesn't Java autoboxing extend to method invocations of methods of the autoboxed types?

前端 未结 8 1102
时光取名叫无心
时光取名叫无心 2020-12-30 18:19

I want to convert a primitive to a string, and I tried:

myInt.toString();

This fails with the error:

int cannot be derefere         


        
8条回答
  •  太阳男子
    2020-12-30 18:55

    Ditto on what Justin said, but you should do this instead:

    Integer.toString(myInt);
    

    It saves an allocation or two and is more readable.

提交回复
热议问题