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 03:49

a is cast to String

No. It is converted to a String. (You can't cast a primitive type to a String, either implicitly or explicitly.)

The details of this conversion are specified in the JLS - 15.18.1.1. The specification states that for a primitive type, the conversion is done "as if" you created an instance of appropriate wrapper type and then called toString() on it. This leaves the Java compiler the option of using other approaches provided that the end result is the same. (For reference types, the conversion turns null into "null" and other non-String types into String by calling toString().)

The JLS states that the concatenation is then performed "as if" by a call to String.concat(...), noting that the JLS explicitly permits optimization of sequences of concatenations. (JLS 15.18.1.2)

查看更多
叛逆
3楼-- · 2019-01-28 03:50

No, it works like String.valueOf( a ) + "", which in turn behaves like new StringBuilder( String.valueOf( a ) ).append( "" ).toString().

The important thing to know is that it's all just done by the compiler, in other words it's syntactic sugar. This is why adding strings together in a loop isn't a good idea for example. (Although modern VMs might have some mechanism to reduce the performance overhead.)

查看更多
Juvenile、少年°
4楼-- · 2019-01-28 03:53

The way this is accomplished in Java is actually a bit simpler than the other answers seem to suggest.

Using the string concatenation operator with a String and an object results in a call to that object's toString() method, then performs string concatenation.

Remember that whenever a primitive is passed to a context where an Object would be valid, Java performs "autoboxing", converting an int to an Integer, a double to a Double, etc.

So you can think of it as autoboxing followed by toString() followed by string concatenation.

At the same time, you should realize that the JLS string concatenation spec allows JVM implementations to optimize away the autoboxing, provided the result is the same, so your JVM may instead use a StringBuilder, or use any other valid JVM bytecode to create the resulting string, so the performance of the string concatenation operator is often better than performing boxing yourself would be.

查看更多
Juvenile、少年°
5楼-- · 2019-01-28 04:00

No.int is a primitive type so it's not an object and can't have any methods.
Read this I think it will be helpful. There are wrapper classes for primitives for example Integer for int. You can call toString() method for them.
Actually String is a special class in java. And you can use + (and not only) operator for Strings and primitives. I think you'll find full answer to your question here.

查看更多
乱世女痞
6楼-- · 2019-01-28 04:01

toString() is a method in Object class, any class that inherits from it will have that method, like Integer or String. But int is a primitive type not an Object, so the method does not exist.

查看更多
时光不老,我们不散
7楼-- · 2019-01-28 04:05

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

True. it doesn't.

+ is overloaded internally. and Java doesn't support method calls on primitives.

查看更多
登录 后发表回答