Implicit cast to string - toString and int + “”

2019-01-28 03:29发布

Why when i use this:

int a = 1;
methodWithParamString(a + "");

a is cast to String, bu i can't use toString() on integer?

int a = 1;
methodWithParamString(a.toString());

Doesn't this: a+"" works like: a.toString() + "" ?

7条回答
戒情不戒烟
2楼-- · 2019-01-28 04:12

Because int a - is not an object, it is primitive type. So it doesn't have any methods. You should use boxing:

Integer objectA = new Integer(a);
objectA.toString();
查看更多
登录 后发表回答