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

前端 未结 8 1110
时光取名叫无心
时光取名叫无心 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:58

    As everyone has pointed out, autoboxing lets you simplify some code, but you cannot pretend that primitives are complex types.

    Also interesting: "autoboxing is a compiler-level hack" in Java. Autoboxing is basically a strange kludge added onto Java. Check out this post for more details about how strange it is.

提交回复
热议问题